| OLD | NEW |
|---|---|
| 1 """Common operations on Posix pathnames. | 1 """Common operations on Posix pathnames. |
| 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 """ |
| 12 | 12 |
| 13 import os | 13 import os |
| 14 import sys | |
| 14 import stat | 15 import stat |
| 15 import genericpath | 16 import genericpath |
| 16 from genericpath import * | 17 from genericpath import * |
| 17 | 18 |
| 18 __all__ = ["normcase","isabs","join","splitdrive","split","splitext", | 19 __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 19 "basename","dirname","commonprefix","getsize","getmtime", | 20 "basename","dirname","commonprefix","getsize","getmtime", |
| 20 "getatime","getctime","islink","exists","lexists","isdir","isfile", | 21 "getatime","getctime","islink","exists","lexists","isdir","isfile", |
| 21 "ismount", "expanduser","expandvars","normpath","abspath", | 22 "ismount", "expanduser","expandvars","normpath","abspath", |
| 22 "samefile","sameopenfile","samestat", | 23 "samefile","sameopenfile","samestat", |
| 23 "curdir","pardir","sep","pathsep","defpath","altsep","extsep", | 24 "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 52 | 53 |
| 53 | 54 |
| 54 # Join pathnames. | 55 # Join pathnames. |
| 55 # Ignore the previous parts if a part is absolute. | 56 # Ignore the previous parts if a part is absolute. |
| 56 # Insert a '/' unless the first part is empty or already ends in '/'. | 57 # Insert a '/' unless the first part is empty or already ends in '/'. |
| 57 | 58 |
| 58 def join(a, *p): | 59 def join(a, *p): |
| 59 """Join two or more pathname components, inserting '/' as needed. | 60 """Join two or more pathname components, inserting '/' as needed. |
| 60 If any component is an absolute path, all previous path components | 61 If any component is an absolute path, all previous path components |
| 61 will be discarded.""" | 62 will be discarded.""" |
| 63 use_bytes = any( isinstance(part, bytes) for part in (a,)+p ) | |
|
GvR
2008/08/22 19:04:51
Style nit: don't put spaces within the ( parenthes
| |
| 62 path = a | 64 path = a |
| 65 sep = '/' | |
| 66 if use_bytes: | |
|
GvR
2008/08/22 19:04:51
Again, I'd recommend changing this so that the arg
| |
| 67 charset = sys.getfilesystemencoding() | |
| 68 sep = sep.encode(charset) | |
| 69 if use_bytes and isinstance(path, str): | |
| 70 path = path.encode(charset) | |
| 63 for b in p: | 71 for b in p: |
| 64 if b.startswith('/'): | 72 if use_bytes and isinstance(b, str): |
| 73 b = b.encode(charset) | |
| 74 if b.startswith(sep): | |
| 65 path = b | 75 path = b |
| 66 elif path == '' or path.endswith('/'): | 76 elif path == '' or path.endswith(sep): |
| 67 path += b | 77 path += b |
| 68 else: | 78 else: |
| 69 path += '/' + b | 79 path += sep + b |
| 70 return path | 80 return path |
| 71 | 81 |
| 72 | 82 |
| 73 # Split a path in head (everything up to the last '/') and tail (the | 83 # 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 | 84 # rest). If the path ends in '/', tail will be empty. If there is no |
| 75 # '/' in the path, head will be empty. | 85 # '/' in the path, head will be empty. |
| 76 # Trailing '/'es are stripped from head unless it is the root. | 86 # Trailing '/'es are stripped from head unless it is the root. |
| 77 | 87 |
| 78 def split(p): | 88 def split(p): |
| 79 """Split a pathname. Returns tuple "(head, tail)" where "tail" is | 89 """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 start_list = abspath(start).split(sep) | 366 start_list = abspath(start).split(sep) |
| 357 path_list = abspath(path).split(sep) | 367 path_list = abspath(path).split(sep) |
| 358 | 368 |
| 359 # Work out how much of the filepath is shared by start and path. | 369 # Work out how much of the filepath is shared by start and path. |
| 360 i = len(commonprefix([start_list, path_list])) | 370 i = len(commonprefix([start_list, path_list])) |
| 361 | 371 |
| 362 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] | 372 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
| 363 if not rel_list: | 373 if not rel_list: |
| 364 return curdir | 374 return curdir |
| 365 return join(*rel_list) | 375 return join(*rel_list) |
| OLD | NEW |