| OLD | NEW |
|---|---|
| 1 """Common operations on Posix pathnames. | 1 """Common operations on Posix pathnames. |
|
GvR
2008/09/30 18:10:41
Also note that os.path.dirname() and os.path.basen
| |
| 2 | 2 |
| 3 Instead of importing this module directly, import os and refer to | 3 Instead of importing this module directly, import os and refer to |
| 4 this module as os.path. The "os.path" name is an alias for this | 4 this module as os.path. The "os.path" name is an alias for this |
| 5 module on Posix systems; on other systems (e.g. Mac, Windows), | 5 module on Posix systems; on other systems (e.g. Mac, Windows), |
| 6 os.path provides the same operations in a manner specific to that | 6 os.path provides the same operations in a manner specific to that |
| 7 platform, and is an alias to another module (e.g. macpath, ntpath). | 7 platform, and is an alias to another module (e.g. macpath, ntpath). |
| 8 | 8 |
| 9 Some of this can actually be useful on non-Posix systems too, e.g. | 9 Some of this can actually be useful on non-Posix systems too, e.g. |
| 10 for manipulation of the pathname component of URLs. | 10 for manipulation of the pathname component of URLs. |
| 11 """ | 11 """ |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 52 |
| 53 | 53 |
| 54 # Join pathnames. | 54 # Join pathnames. |
| 55 # Ignore the previous parts if a part is absolute. | 55 # Ignore the previous parts if a part is absolute. |
| 56 # Insert a '/' unless the first part is empty or already ends in '/'. | 56 # Insert a '/' unless the first part is empty or already ends in '/'. |
| 57 | 57 |
| 58 def join(a, *p): | 58 def join(a, *p): |
| 59 """Join two or more pathname components, inserting '/' as needed. | 59 """Join two or more pathname components, inserting '/' as needed. |
| 60 If any component is an absolute path, all previous path components | 60 If any component is an absolute path, all previous path components |
| 61 will be discarded.""" | 61 will be discarded.""" |
| 62 if isinstance(a, bytes): | |
| 63 sep = b'/' | |
| 64 else: | |
| 65 sep = '/' | |
| 62 path = a | 66 path = a |
| 63 for b in p: | 67 for b in p: |
| 64 if b.startswith('/'): | 68 if b.startswith(sep): |
| 65 path = b | 69 path = b |
| 66 elif path == '' or path.endswith('/'): | 70 elif not path or path.endswith(sep): |
| 67 path += b | 71 path += b |
| 68 else: | 72 else: |
| 69 path += '/' + b | 73 path += sep + b |
| 70 return path | 74 return path |
| 71 | 75 |
| 72 | 76 |
| 73 # Split a path in head (everything up to the last '/') and tail (the | 77 # Split a path in head (everything up to the last '/') and tail (the |
| 74 # rest). If the path ends in '/', tail will be empty. If there is no | 78 # rest). If the path ends in '/', tail will be empty. If there is no |
| 75 # '/' in the path, head will be empty. | 79 # '/' in the path, head will be empty. |
| 76 # Trailing '/'es are stripped from head unless it is the root. | 80 # Trailing '/'es are stripped from head unless it is the root. |
| 77 | 81 |
| 78 def split(p): | 82 def split(p): |
| 79 """Split a pathname. Returns tuple "(head, tail)" where "tail" is | 83 """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
| 80 everything after the final slash. Either part may be empty.""" | 84 everything after the final slash. Either part may be empty.""" |
| 81 i = p.rfind('/') + 1 | 85 if isinstance(p, bytes): |
| 86 sep = b'/' | |
| 87 else: | |
| 88 sep = '/' | |
| 89 i = p.rfind(sep) + 1 | |
| 82 head, tail = p[:i], p[i:] | 90 head, tail = p[:i], p[i:] |
| 83 if head and head != '/'*len(head): | 91 if head and head != sep*len(head): |
| 84 head = head.rstrip('/') | 92 head = head.rstrip(sep) |
| 85 return head, tail | 93 return head, tail |
| 86 | 94 |
| 87 | 95 |
| 88 # Split a path in root and extension. | 96 # Split a path in root and extension. |
| 89 # The extension is everything starting at the last dot in the last | 97 # The extension is everything starting at the last dot in the last |
| 90 # pathname component; the root is everything before that. | 98 # pathname component; the root is everything before that. |
| 91 # It is always true that root + ext == p. | 99 # It is always true that root + ext == p. |
| 92 | 100 |
| 93 def splitext(p): | 101 def splitext(p): |
| 94 return genericpath._splitext(p, sep, altsep, extsep) | 102 return genericpath._splitext(p, sep, altsep, extsep) |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 start_list = abspath(start).split(sep) | 364 start_list = abspath(start).split(sep) |
| 357 path_list = abspath(path).split(sep) | 365 path_list = abspath(path).split(sep) |
| 358 | 366 |
| 359 # Work out how much of the filepath is shared by start and path. | 367 # Work out how much of the filepath is shared by start and path. |
| 360 i = len(commonprefix([start_list, path_list])) | 368 i = len(commonprefix([start_list, path_list])) |
| 361 | 369 |
| 362 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] | 370 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
| 363 if not rel_list: | 371 if not rel_list: |
| 364 return curdir | 372 return curdir |
| 365 return join(*rel_list) | 373 return join(*rel_list) |
| OLD | NEW |