diff options
author | Walter Dörwald <walter@livinglogic.de> | 2003-06-17 13:13:40 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2003-06-17 13:13:40 (GMT) |
commit | 77cdeaff556447a980fe8632e8cd010499ade2d0 (patch) | |
tree | 6ac576062a8f81a98453814fcacaeadbec6187b4 /Lib/posixpath.py | |
parent | 364ca40c2a053718f67c2032769de1f1fd76bb22 (diff) | |
download | cpython-77cdeaff556447a980fe8632e8cd010499ade2d0.zip cpython-77cdeaff556447a980fe8632e8cd010499ade2d0.tar.gz cpython-77cdeaff556447a980fe8632e8cd010499ade2d0.tar.bz2 |
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
struct_passwd attributes and +=.
From SF patch #755245.
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r-- | Lib/posixpath.py | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 50073dc..1c63af8 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 |