Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 1 | # Module 'ntpath' -- common operations on WinNT/Win95 pathnames |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 2 | """Common pathname manipulations, WindowsNT/95 version. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 3 | |
| 4 | Instead of importing this module directly, import os and refer to this |
| 5 | module as os.path. |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 6 | """ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 7 | |
| 8 | import os |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 9 | import sys |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 10 | import stat |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | import genericpath |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | from genericpath import * |
Skip Montanaro | 4d5d5bf | 2000-07-13 01:01:03 +0000 | [diff] [blame] | 13 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 14 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 15 | "basename","dirname","commonprefix","getsize","getmtime", |
Georg Brandl | f0de6a1 | 2005-08-22 18:02:59 +0000 | [diff] [blame] | 16 | "getatime","getctime", "islink","exists","lexists","isdir","isfile", |
Benjamin Peterson | d71ca41 | 2008-05-08 23:44:58 +0000 | [diff] [blame] | 17 | "ismount", "expanduser","expandvars","normpath","abspath", |
Georg Brandl | f0de6a1 | 2005-08-22 18:02:59 +0000 | [diff] [blame] | 18 | "splitunc","curdir","pardir","sep","pathsep","defpath","altsep", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | "extsep","devnull","realpath","supports_unicode_filenames","relpath"] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 20 | |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 21 | # strings representing various path-related bits and pieces |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 22 | # These are primarily for export; internally, they are hardcoded. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 23 | curdir = '.' |
| 24 | pardir = '..' |
| 25 | extsep = '.' |
| 26 | sep = '\\' |
| 27 | pathsep = ';' |
Skip Montanaro | 9ddac3e | 2003-03-28 22:23:24 +0000 | [diff] [blame] | 28 | altsep = '/' |
Andrew MacIntyre | 437966c | 2003-02-17 09:17:50 +0000 | [diff] [blame] | 29 | defpath = '.;C:\\bin' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 30 | if 'ce' in sys.builtin_module_names: |
| 31 | defpath = '\\Windows' |
| 32 | elif 'os2' in sys.builtin_module_names: |
Andrew MacIntyre | 437966c | 2003-02-17 09:17:50 +0000 | [diff] [blame] | 33 | # OS/2 w/ VACPP |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 34 | altsep = '/' |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 35 | devnull = 'nul' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 36 | |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 37 | def _get_empty(path): |
| 38 | if isinstance(path, bytes): |
| 39 | return b'' |
| 40 | else: |
| 41 | return '' |
| 42 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 43 | def _get_sep(path): |
| 44 | if isinstance(path, bytes): |
| 45 | return b'\\' |
| 46 | else: |
| 47 | return '\\' |
| 48 | |
| 49 | def _get_altsep(path): |
| 50 | if isinstance(path, bytes): |
| 51 | return b'/' |
| 52 | else: |
| 53 | return '/' |
| 54 | |
| 55 | def _get_bothseps(path): |
| 56 | if isinstance(path, bytes): |
| 57 | return b'\\/' |
| 58 | else: |
| 59 | return '\\/' |
| 60 | |
| 61 | def _get_dot(path): |
| 62 | if isinstance(path, bytes): |
| 63 | return b'.' |
| 64 | else: |
| 65 | return '.' |
| 66 | |
| 67 | def _get_colon(path): |
| 68 | if isinstance(path, bytes): |
| 69 | return b':' |
| 70 | else: |
| 71 | return ':' |
| 72 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 73 | # Normalize the case of a pathname and map slashes to backslashes. |
| 74 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 75 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 77 | def normcase(s): |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 78 | """Normalize case of pathname. |
| 79 | |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 80 | Makes all characters lowercase and all slashes into backslashes.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 81 | return s.replace(_get_altsep(s), _get_sep(s)).lower() |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 83 | |
Fred Drake | ef0b5dd | 2000-02-17 17:30:40 +0000 | [diff] [blame] | 84 | # Return whether a path is absolute. |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 85 | # Trivial in Posix, harder on Windows. |
| 86 | # For Windows it is absolute if it starts with a slash or backslash (current |
| 87 | # volume), or if a pathname after the volume-letter-and-colon or UNC-resource |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 88 | # starts with a slash or backslash. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 89 | |
| 90 | def isabs(s): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 91 | """Test whether a path is absolute""" |
| 92 | s = splitdrive(s)[1] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 93 | return len(s) > 0 and s[:1] in _get_bothseps(s) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 94 | |
| 95 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 96 | # Join two (or more) paths. |
| 97 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 98 | def join(a, *p): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 99 | """Join two or more pathname components, inserting "\\" as needed. |
| 100 | If any component is an absolute path, all previous path components |
| 101 | will be discarded.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 102 | sep = _get_sep(a) |
| 103 | seps = _get_bothseps(a) |
| 104 | colon = _get_colon(a) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 105 | path = a |
| 106 | for b in p: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 107 | b_wins = 0 # set to 1 iff b makes path irrelevant |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 108 | if not path: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 109 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 110 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 111 | elif isabs(b): |
| 112 | # This probably wipes out path so far. However, it's more |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 113 | # complicated if path begins with a drive letter. You get a+b |
| 114 | # (minus redundant slashes) in these four cases: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 115 | # 1. join('c:', '/a') == 'c:/a' |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 116 | # 2. join('//computer/share', '/a') == '//computer/share/a' |
| 117 | # 3. join('c:/', '/a') == 'c:/a' |
| 118 | # 4. join('//computer/share/', '/a') == '//computer/share/a' |
| 119 | # But b wins in all of these cases: |
| 120 | # 5. join('c:/a', '/b') == '/b' |
| 121 | # 6. join('//computer/share/a', '/b') == '/b' |
| 122 | # 7. join('c:', 'd:/') == 'd:/' |
| 123 | # 8. join('c:', '//computer/share/') == '//computer/share/' |
| 124 | # 9. join('//computer/share', 'd:/') == 'd:/' |
| 125 | # 10. join('//computer/share', '//computer/share/') == '//computer/share/' |
| 126 | # 11. join('c:/', 'd:/') == 'd:/' |
| 127 | # 12. join('c:/', '//computer/share/') == '//computer/share/' |
| 128 | # 13. join('//computer/share/', 'd:/') == 'd:/' |
| 129 | # 14. join('//computer/share/', '//computer/share/') == '//computer/share/' |
| 130 | b_prefix, b_rest = splitdrive(b) |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 131 | |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 132 | # if b has a prefix, it always wins. |
| 133 | if b_prefix: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 134 | b_wins = 1 |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 135 | else: |
| 136 | # b doesn't have a prefix. |
| 137 | # but isabs(b) returned true. |
| 138 | # and therefore b_rest[0] must be a slash. |
| 139 | # (but let's check that.) |
| 140 | assert(b_rest and b_rest[0] in seps) |
| 141 | |
| 142 | # so, b still wins if path has a rest that's more than a sep. |
| 143 | # you get a+b if path_rest is empty or only has a sep. |
| 144 | # (see cases 1-4 for times when b loses.) |
| 145 | path_rest = splitdrive(path)[1] |
| 146 | b_wins = path_rest and path_rest not in seps |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 147 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 148 | if b_wins: |
| 149 | path = b |
| 150 | else: |
| 151 | # Join, and ensure there's a separator. |
| 152 | assert len(path) > 0 |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 153 | if path[-1:] in seps: |
| 154 | if b and b[:1] in seps: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 155 | path += b[1:] |
| 156 | else: |
| 157 | path += b |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 158 | elif path[-1:] == colon: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 159 | path += b |
| 160 | elif b: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 161 | if b[:1] in seps: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 162 | path += b |
| 163 | else: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 164 | path += sep + b |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 165 | else: |
| 166 | # path is not empty and does not end with a backslash, |
| 167 | # but b is empty; since, e.g., split('a/') produces |
| 168 | # ('a', ''), it's best if join() adds a backslash in |
| 169 | # this case. |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 170 | path += sep |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 172 | return path |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 173 | |
| 174 | |
| 175 | # Split a path in a drive specification (a drive letter followed by a |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 176 | # colon) and the path specification. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 177 | # It is always true that drivespec + pathspec == p |
| 178 | def splitdrive(p): |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 179 | """Split a pathname into drive/UNC sharepoint and relative path specifiers. |
| 180 | Returns a 2-tuple (drive_or_unc, path); either part may be empty. |
| 181 | |
| 182 | If you assign |
| 183 | result = splitdrive(p) |
| 184 | It is always true that: |
| 185 | result[0] + result[1] == p |
| 186 | |
| 187 | If the path contained a drive letter, drive_or_unc will contain everything |
| 188 | up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir") |
| 189 | |
| 190 | If the path contained a UNC path, the drive_or_unc will contain the host name |
| 191 | and share up to but not including the fourth directory separator character. |
| 192 | e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir") |
| 193 | |
| 194 | Paths cannot contain both a drive letter and a UNC path. |
| 195 | |
| 196 | """ |
| 197 | empty = _get_empty(p) |
| 198 | if len(p) > 1: |
| 199 | sep = _get_sep(p) |
| 200 | normp = normcase(p) |
| 201 | if (normp[0:2] == sep*2) and (normp[2:3] != sep): |
| 202 | # is a UNC path: |
| 203 | # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path |
| 204 | # \\machine\mountpoint\directory\etc\... |
| 205 | # directory ^^^^^^^^^^^^^^^ |
| 206 | index = normp.find(sep, 2) |
| 207 | if index == -1: |
| 208 | return empty, p |
| 209 | index2 = normp.find(sep, index + 1) |
| 210 | # a UNC path can't have two slashes in a row |
| 211 | # (after the initial two) |
| 212 | if index2 == index + 1: |
| 213 | return empty, p |
| 214 | if index2 == -1: |
| 215 | index2 = len(p) |
| 216 | return p[:index2], p[index2:] |
| 217 | if normp[1:2] == _get_colon(p): |
| 218 | return p[:2], p[2:] |
| 219 | return empty, p |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 220 | |
| 221 | |
| 222 | # Parse UNC paths |
| 223 | def splitunc(p): |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 224 | """Deprecated since Python 3.1. Please use splitdrive() instead; |
| 225 | it now handles UNC paths. |
| 226 | |
| 227 | Split a pathname into UNC mount point and relative path specifiers. |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 228 | |
| 229 | Return a 2-tuple (unc, rest); either part may be empty. |
| 230 | If unc is not empty, it has the form '//host/mount' (or similar |
| 231 | using backslashes). unc+rest is always the input path. |
| 232 | Paths containing drive letters never have an UNC part. |
| 233 | """ |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 234 | import warnings |
| 235 | warnings.warn("ntpath.splitunc is deprecated, use ntpath.splitdrive instead", |
| 236 | PendingDeprecationWarning) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 237 | sep = _get_sep(p) |
| 238 | if not p[1:2]: |
| 239 | return p[:0], p # Drive letter present |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 240 | firstTwo = p[0:2] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 241 | if normcase(firstTwo) == sep + sep: |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 242 | # is a UNC path: |
| 243 | # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter |
| 244 | # \\machine\mountpoint\directories... |
| 245 | # directory ^^^^^^^^^^^^^^^ |
| 246 | normp = normcase(p) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 247 | index = normp.find(sep, 2) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 248 | if index == -1: |
| 249 | ##raise RuntimeError, 'illegal UNC path: "' + p + '"' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 250 | return (p[:0], p) |
| 251 | index = normp.find(sep, index + 1) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 252 | if index == -1: |
| 253 | index = len(p) |
| 254 | return p[:index], p[index:] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 255 | return p[:0], p |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 256 | |
| 257 | |
| 258 | # Split a path in head (everything up to the last '/') and tail (the |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 259 | # rest). After the trailing '/' is stripped, the invariant |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 260 | # join(head, tail) == p holds. |
| 261 | # The resulting head won't end in '/' unless it is the root. |
| 262 | |
| 263 | def split(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 264 | """Split a pathname. |
| 265 | |
| 266 | Return tuple (head, tail) where tail is everything after the final slash. |
| 267 | Either part may be empty.""" |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 268 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 269 | seps = _get_bothseps(p) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 270 | d, p = splitdrive(p) |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 271 | # set i to index beyond p's last slash |
| 272 | i = len(p) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 273 | while i and p[i-1] not in seps: |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 274 | i = i - 1 |
| 275 | head, tail = p[:i], p[i:] # now tail has no slashes |
| 276 | # remove trailing slashes from head, unless it's all slashes |
| 277 | head2 = head |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 278 | while head2 and head2[-1:] in seps: |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 279 | head2 = head2[:-1] |
| 280 | head = head2 or head |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 281 | return d + head, tail |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 282 | |
| 283 | |
| 284 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 285 | # The extension is everything starting at the last dot in the last |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 286 | # pathname component; the root is everything before that. |
| 287 | # It is always true that root + ext == p. |
| 288 | |
| 289 | def splitext(p): |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 290 | return genericpath._splitext(p, _get_sep(p), _get_altsep(p), |
| 291 | _get_dot(p)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 292 | splitext.__doc__ = genericpath._splitext.__doc__ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 293 | |
| 294 | |
| 295 | # Return the tail (basename) part of a path. |
| 296 | |
| 297 | def basename(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 298 | """Returns the final component of a pathname""" |
| 299 | return split(p)[1] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 300 | |
| 301 | |
| 302 | # Return the head (dirname) part of a path. |
| 303 | |
| 304 | def dirname(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 305 | """Returns the directory component of a pathname""" |
| 306 | return split(p)[0] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 308 | # Is a path a symbolic link? |
| 309 | # This will always return false on systems where posix.lstat doesn't exist. |
| 310 | |
| 311 | def islink(path): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 312 | """Test for symbolic link. |
| 313 | On WindowsNT/95 and OS/2 always returns false |
| 314 | """ |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 315 | return False |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 316 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 317 | # alias exists to lexists |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 318 | lexists = exists |
| 319 | |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 320 | # Is a path a mount point? Either a root (with or without drive letter) |
| 321 | # or an UNC path with at most a / or \ after the mount point. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 322 | |
| 323 | def ismount(path): |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 324 | """Test whether a path is a mount point (defined as root of drive)""" |
Benjamin Peterson | 48e2478 | 2009-03-29 13:02:52 +0000 | [diff] [blame] | 325 | seps = _get_bothseps(path) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 326 | root, rest = splitdrive(path) |
| 327 | if root and root[0] in seps: |
| 328 | return (not rest) or (rest in seps) |
| 329 | return rest in seps |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 330 | |
| 331 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 332 | # Expand paths beginning with '~' or '~user'. |
| 333 | # '~' means $HOME; '~user' means that user's home directory. |
| 334 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 335 | # the path is returned unchanged (leaving error reporting to whatever |
| 336 | # function is called with the expanded path as argument). |
| 337 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 338 | # (A function should also be defined to do full *sh-style environment |
| 339 | # variable expansion.) |
| 340 | |
| 341 | def expanduser(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 342 | """Expand ~ and ~user constructs. |
| 343 | |
| 344 | If user or $HOME is unknown, do nothing.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 345 | if isinstance(path, bytes): |
| 346 | tilde = b'~' |
| 347 | else: |
| 348 | tilde = '~' |
| 349 | if not path.startswith(tilde): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 350 | return path |
| 351 | i, n = 1, len(path) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 352 | while i < n and path[i] not in _get_bothseps(path): |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 353 | i = i + 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 354 | |
| 355 | if 'HOME' in os.environ: |
| 356 | userhome = os.environ['HOME'] |
| 357 | elif 'USERPROFILE' in os.environ: |
| 358 | userhome = os.environ['USERPROFILE'] |
| 359 | elif not 'HOMEPATH' in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 360 | return path |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 361 | else: |
| 362 | try: |
| 363 | drive = os.environ['HOMEDRIVE'] |
| 364 | except KeyError: |
| 365 | drive = '' |
| 366 | userhome = join(drive, os.environ['HOMEPATH']) |
| 367 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 368 | if isinstance(path, bytes): |
| 369 | userhome = userhome.encode(sys.getfilesystemencoding()) |
| 370 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 371 | if i != 1: #~user |
| 372 | userhome = join(dirname(userhome), path[1:i]) |
| 373 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 374 | return userhome + path[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 375 | |
| 376 | |
| 377 | # Expand paths containing shell variable substitutions. |
| 378 | # The following rules apply: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 379 | # - no expansion within single quotes |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 380 | # - '$$' is translated into '$' |
| 381 | # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2% |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 382 | # - ${varname} is accepted. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 383 | # - $varname is accepted. |
| 384 | # - %varname% is accepted. |
| 385 | # - varnames can be made out of letters, digits and the characters '_-' |
| 386 | # (though is not verifed in the ${varname} and %varname% cases) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 387 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 388 | # XXX except '^|<>='. |
| 389 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 390 | def expandvars(path): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 391 | """Expand shell variables of the forms $var, ${var} and %var%. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 392 | |
| 393 | Unknown variables are left unchanged.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 394 | if isinstance(path, bytes): |
| 395 | if ord('$') not in path and ord('%') not in path: |
| 396 | return path |
| 397 | import string |
| 398 | varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii') |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 399 | quote = b'\'' |
| 400 | percent = b'%' |
| 401 | brace = b'{' |
| 402 | dollar = b'$' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 403 | else: |
| 404 | if '$' not in path and '%' not in path: |
| 405 | return path |
| 406 | import string |
| 407 | varchars = string.ascii_letters + string.digits + '_-' |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 408 | quote = '\'' |
| 409 | percent = '%' |
| 410 | brace = '{' |
| 411 | dollar = '$' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 412 | res = path[:0] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 413 | index = 0 |
| 414 | pathlen = len(path) |
| 415 | while index < pathlen: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 416 | c = path[index:index+1] |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 417 | if c == quote: # no expansion within single quotes |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 418 | path = path[index + 1:] |
| 419 | pathlen = len(path) |
| 420 | try: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 421 | index = path.index(c) |
| 422 | res = res + c + path[:index + 1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 423 | except ValueError: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 424 | res = res + path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 425 | index = pathlen - 1 |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 426 | elif c == percent: # variable or '%' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 427 | if path[index + 1:index + 2] == percent: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 428 | res = res + c |
| 429 | index = index + 1 |
| 430 | else: |
| 431 | path = path[index+1:] |
| 432 | pathlen = len(path) |
| 433 | try: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 434 | index = path.index(percent) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 435 | except ValueError: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 436 | res = res + percent + path |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 437 | index = pathlen - 1 |
| 438 | else: |
| 439 | var = path[:index] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 440 | if isinstance(path, bytes): |
| 441 | var = var.decode('ascii') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 442 | if var in os.environ: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 443 | value = os.environ[var] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 444 | else: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 445 | value = '%' + var + '%' |
| 446 | if isinstance(path, bytes): |
| 447 | value = value.encode('ascii') |
| 448 | res = res + value |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 449 | elif c == dollar: # variable or '$$' |
| 450 | if path[index + 1:index + 2] == dollar: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 451 | res = res + c |
| 452 | index = index + 1 |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 453 | elif path[index + 1:index + 2] == brace: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 454 | path = path[index+2:] |
| 455 | pathlen = len(path) |
| 456 | try: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 457 | if isinstance(path, bytes): |
| 458 | index = path.index(b'}') |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 459 | else: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 460 | index = path.index('}') |
| 461 | var = path[:index] |
| 462 | if isinstance(path, bytes): |
| 463 | var = var.decode('ascii') |
| 464 | if var in os.environ: |
| 465 | value = os.environ[var] |
| 466 | else: |
| 467 | value = '${' + var + '}' |
| 468 | if isinstance(path, bytes): |
| 469 | value = value.encode('ascii') |
| 470 | res = res + value |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 471 | except ValueError: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 472 | if isinstance(path, bytes): |
| 473 | res = res + b'${' + path |
| 474 | else: |
| 475 | res = res + '${' + path |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 476 | index = pathlen - 1 |
| 477 | else: |
| 478 | var = '' |
| 479 | index = index + 1 |
| 480 | c = path[index:index + 1] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 481 | while c and c in varchars: |
| 482 | if isinstance(path, bytes): |
| 483 | var = var + c.decode('ascii') |
| 484 | else: |
| 485 | var = var + c |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 486 | index = index + 1 |
| 487 | c = path[index:index + 1] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 488 | if var in os.environ: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 489 | value = os.environ[var] |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 490 | else: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 491 | value = '$' + var |
| 492 | if isinstance(path, bytes): |
| 493 | value = value.encode('ascii') |
| 494 | res = res + value |
| 495 | if c: |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 496 | index = index - 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 497 | else: |
| 498 | res = res + c |
| 499 | index = index + 1 |
| 500 | return res |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 501 | |
| 502 | |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 503 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B. |
Guido van Rossum | 3df7b5a | 1996-08-26 16:35:26 +0000 | [diff] [blame] | 504 | # Previously, this function also truncated pathnames to 8+3 format, |
| 505 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 506 | |
| 507 | def normpath(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 508 | """Normalize path, eliminating double slashes, etc.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 509 | sep = _get_sep(path) |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 510 | dotdot = _get_dot(path) * 2 |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 511 | path = path.replace(_get_altsep(path), sep) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 512 | prefix, path = splitdrive(path) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 513 | |
| 514 | # collapse initial backslashes |
| 515 | if path.startswith(sep): |
| 516 | prefix = prefix + sep |
| 517 | path = path.lstrip(sep) |
| 518 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 519 | comps = path.split(sep) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 520 | i = 0 |
| 521 | while i < len(comps): |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 522 | if not comps[i] or comps[i] == _get_dot(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 523 | del comps[i] |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 524 | elif comps[i] == dotdot: |
| 525 | if i > 0 and comps[i-1] != dotdot: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 526 | del comps[i-1:i+1] |
| 527 | i -= 1 |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 528 | elif i == 0 and prefix.endswith(_get_sep(path)): |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 529 | del comps[i] |
| 530 | else: |
| 531 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 532 | else: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 533 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 534 | # If the path is now empty, substitute '.' |
| 535 | if not prefix and not comps: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 536 | comps.append(_get_dot(path)) |
| 537 | return prefix + sep.join(comps) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 538 | |
| 539 | |
| 540 | # Return an absolute path. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 541 | try: |
| 542 | from nt import _getfullpathname |
Mark Hammond | f717f05 | 2002-01-17 00:44:26 +0000 | [diff] [blame] | 543 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 544 | except ImportError: # not running on Windows - mock up something sensible |
| 545 | def abspath(path): |
| 546 | """Return the absolute version of a path.""" |
| 547 | if not isabs(path): |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 548 | if isinstance(path, bytes): |
| 549 | cwd = os.getcwdb() |
| 550 | else: |
| 551 | cwd = os.getcwd() |
| 552 | path = join(cwd, path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 553 | return normpath(path) |
| 554 | |
| 555 | else: # use native Windows method on Windows |
| 556 | def abspath(path): |
| 557 | """Return the absolute version of a path.""" |
| 558 | |
| 559 | if path: # Empty path must return current working directory. |
| 560 | try: |
| 561 | path = _getfullpathname(path) |
| 562 | except WindowsError: |
| 563 | pass # Bad path - return unchanged. |
| 564 | else: |
| 565 | path = os.getcwd() |
| 566 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 567 | |
| 568 | # realpath is a no-op on systems without islink support |
| 569 | realpath = abspath |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 570 | # Win9x family and earlier have no Unicode filename support. |
Tim Peters | 26bc25a | 2002-10-09 07:56:04 +0000 | [diff] [blame] | 571 | supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and |
| 572 | sys.getwindowsversion()[3] >= 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 573 | |
| 574 | def relpath(path, start=curdir): |
| 575 | """Return a relative version of a path""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 576 | sep = _get_sep(path) |
| 577 | |
| 578 | if start is curdir: |
| 579 | start = _get_dot(path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 580 | |
| 581 | if not path: |
| 582 | raise ValueError("no path specified") |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 583 | |
| 584 | start_abs = abspath(normpath(start)) |
| 585 | path_abs = abspath(normpath(path)) |
| 586 | start_drive, start_rest = splitdrive(start_abs) |
| 587 | path_drive, path_rest = splitdrive(path_abs) |
| 588 | if start_drive != path_drive: |
| 589 | error = "path is on mount '{0}', start on mount '{1}'".format( |
| 590 | path_drive, start_drive) |
| 591 | raise ValueError(error) |
| 592 | |
| 593 | start_list = [x for x in start_rest.split(sep) if x] |
| 594 | path_list = [x for x in path_rest.split(sep) if x] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 595 | # Work out how much of the filepath is shared by start and path. |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 596 | i = 0 |
| 597 | for e1, e2 in zip(start_list, path_list): |
| 598 | if e1 != e2: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 599 | break |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 600 | i += 1 |
| 601 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 602 | if isinstance(path, bytes): |
| 603 | pardir = b'..' |
| 604 | else: |
| 605 | pardir = '..' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 606 | rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 607 | if not rel_list: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 608 | return _get_dot(path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 609 | return join(*rel_list) |