|
1 | | -# Module 'path' -- common operations on POSIX pathnames |
| 1 | +# Module 'posixpath' -- common operations on POSIX pathnames |
2 | 2 |
|
3 | 3 | import posix |
4 | 4 | import stat |
@@ -31,20 +31,24 @@ def join(a, b): |
31 | 31 | return a + '/' + b |
32 | 32 |
|
33 | 33 |
|
34 | | -# Split a path in head (empty or ending in '/') and tail (no '/'). |
35 | | -# The tail will be empty if the path ends in '/'. |
36 | | -# It is always true that head + tail == p; also join(head, tail) == p. |
37 | | -# Note that because head ends in '/', if you want to find all components |
38 | | -# of a path by repeatedly getting the head, you will have to strip off |
39 | | -# the trailing '/' yourself (another function should be defined to |
40 | | -# split an entire path into components.) |
| 34 | +# Split a path in head (everything up to the last '/') and tail (the |
| 35 | +# rest). If the original path ends in '/' but is not the root, this |
| 36 | +# '/' is stripped. After the trailing '/' is stripped, the invariant |
| 37 | +# join(head, tail) == p holds. |
| 38 | +# The resulting head won't end in '/' unless it is the root. |
41 | 39 |
|
42 | 40 | def split(p): |
| 41 | + if p[-1:] == '/' and p <> '/'*len(p): |
| 42 | + while p[-1] == '/': |
| 43 | + p = p[:-1] |
43 | 44 | head, tail = '', '' |
44 | 45 | for c in p: |
45 | 46 | tail = tail + c |
46 | 47 | if c == '/': |
47 | 48 | head, tail = head + tail, '' |
| 49 | + if head[-1:] == '/' and head <> '/'*len(head): |
| 50 | + while head[-1] == '/': |
| 51 | + head = head[:-1] |
48 | 52 | return head, tail |
49 | 53 |
|
50 | 54 |
|
@@ -119,7 +123,7 @@ def isdir(path): |
119 | 123 | return stat.S_ISDIR(st[stat.ST_MODE]) |
120 | 124 |
|
121 | 125 |
|
122 | | -# Is a path a regulat file? |
| 126 | +# Is a path a regular file? |
123 | 127 | # This follows symbolic links, so both islink() and isdir() can be true |
124 | 128 | # for the same path. |
125 | 129 |
|
|
0 commit comments