diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 50073dc99d5..1c63af8d47e 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -45,7 +45,7 @@ def normcase(s): def isabs(s): """Test whether a path is absolute""" - return s[:1] == '/' + return s.startswith('/') # Join pathnames. @@ -56,12 +56,12 @@ def join(a, *p): """Join two or more pathname components, inserting '/' as needed""" path = a for b in p: - if b[:1] == '/': + if b.startswith('/'): path = b - elif path == '' or path[-1:] == '/': - path = path + b + elif path == '' or path.endswith('/'): + path += b else: - path = path + '/' + b + path += '/' + b return path @@ -76,8 +76,7 @@ def split(p): i = p.rfind('/') + 1 head, tail = p[:i], p[i:] if head and head != '/'*len(head): - while head[-1] == '/': - head = head[:-1] + head = head.rstrip('/') return head, tail @@ -129,7 +128,8 @@ def commonprefix(m): for i in range(len(prefix)): if prefix[:i+1] != item[:i+1]: prefix = prefix[:i] - if i == 0: return '' + if i == 0: + return '' break return prefix @@ -301,15 +301,15 @@ def walk(top, func, arg): def expanduser(path): """Expand ~ and ~user constructions. If user or $HOME is unknown, do nothing.""" - if path[:1] != '~': + if not path.startswith('~'): return path i, n = 1, len(path) while i < n and path[i] != '/': - i = i + 1 + i += 1 if i == 1: if not 'HOME' in os.environ: import pwd - userhome = pwd.getpwuid(os.getuid())[5] + userhome = pwd.getpwuid(os.getuid()).pw_dir else: userhome = os.environ['HOME'] else: @@ -318,8 +318,9 @@ def expanduser(path): pwent = pwd.getpwnam(path[1:i]) except KeyError: return path - userhome = pwent[5] - if userhome[-1:] == '/': i = i + 1 + userhome = pwent.pw_dir + if userhome.endswith('/'): + i += 1 return userhome + path[i:] @@ -345,13 +346,13 @@ def expandvars(path): break i, j = m.span(0) name = m.group(1) - if name[:1] == '{' and name[-1:] == '}': + if name.startswith('{') and name.endswith('}'): name = name[1:-1] if name in os.environ: tail = path[j:] path = path[:i] + os.environ[name] i = len(path) - path = path + tail + path += tail else: i = j return path