rationalized os.path.split() so split "/a/" yields "/a", ""

This commit is contained in:
Guido van Rossum 1995-09-01 20:32:21 +00:00
parent 6655c4e2d2
commit a89b1bade5

View file

@ -32,21 +32,15 @@ def join(a, b):
# Split a path in head (everything up to the last '/') and tail (the # Split a path in head (everything up to the last '/') and tail (the
# rest). If the original path ends in '/' but is not the root, this # rest). If the path ends in '/', tail will be empty. If there is no
# '/' is stripped. After the trailing '/' is stripped, the invariant # '/' in the path, head will be empty.
# join(head, tail) == p holds. # Trailing '/'es are stripped from head unless it is the root.
# The resulting head won't end in '/' unless it is the root.
def split(p): def split(p):
if p[-1:] == '/' and p <> '/'*len(p): import string
while p[-1] == '/': i = string.rfind(p, '/') + 1
p = p[:-1] head, tail = p[:i], p[i:]
head, tail = '', '' if head and head <> '/'*len(head):
for c in p:
tail = tail + c
if c == '/':
head, tail = head + tail, ''
if head[-1:] == '/' and head <> '/'*len(head):
while head[-1] == '/': while head[-1] == '/':
head = head[:-1] head = head[:-1]
return head, tail return head, tail