summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-09-01 20:32:21 (GMT)
committerGuido van Rossum <guido@python.org>1995-09-01 20:32:21 (GMT)
commita89b1bade562cf4ed5e03a67d151ce49c16a172a (patch)
tree2aeed3bcabc233b3f13a15cd5a8639c8afa17f0c
parent6655c4e2d230519f14f2f6a6311fe5157e636049 (diff)
downloadcpython-a89b1bade562cf4ed5e03a67d151ce49c16a172a.zip
cpython-a89b1bade562cf4ed5e03a67d151ce49c16a172a.tar.gz
cpython-a89b1bade562cf4ed5e03a67d151ce49c16a172a.tar.bz2
rationalized os.path.split() so split "/a/" yields "/a", ""
-rw-r--r--Lib/posixpath.py20
1 files changed, 7 insertions, 13 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 05cc92a..c288f3b 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -32,21 +32,15 @@ def join(a, b):
# 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
-# '/' is stripped. After the trailing '/' is stripped, the invariant
-# join(head, tail) == p holds.
-# The resulting head won't end in '/' unless it is the root.
+# rest). If the path ends in '/', tail will be empty. If there is no
+# '/' in the path, head will be empty.
+# Trailing '/'es are stripped from head unless it is the root.
def split(p):
- if p[-1:] == '/' and p <> '/'*len(p):
- while p[-1] == '/':
- p = p[:-1]
- head, tail = '', ''
- for c in p:
- tail = tail + c
- if c == '/':
- head, tail = head + tail, ''
- if head[-1:] == '/' and head <> '/'*len(head):
+ import string
+ i = string.rfind(p, '/') + 1
+ head, tail = p[:i], p[i:]
+ if head and head <> '/'*len(head):
while head[-1] == '/':
head = head[:-1]
return head, tail