blob: 579673d34e8d1a7857ad4c2b00f914e26d786164 [file] [log] [blame]
Guido van Rossum15e22e11997-12-05 19:03:01 +00001# Module 'ntpath' -- common operations on WinNT/Win95 pathnames
Tim Peters2344fae2001-01-15 00:50:52 +00002"""Common pathname manipulations, WindowsNT/95 version.
Guido van Rossum534972b1999-02-03 17:20:50 +00003
4Instead of importing this module directly, import os and refer to this
5module as os.path.
Guido van Rossum15e22e11997-12-05 19:03:01 +00006"""
Guido van Rossum555915a1994-02-24 11:32:59 +00007
8import os
Mark Hammond8696ebc2002-10-08 02:44:31 +00009import sys
Christian Heimes05e8be12008-02-23 18:30:17 +000010import stat
Guido van Rossumd8faa362007-04-27 19:54:29 +000011import genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +000012from genericpath import *
Skip Montanaro4d5d5bf2000-07-13 01:01:03 +000013
Skip Montanaro269b83b2001-02-06 01:07:02 +000014__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
15 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000016 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
Benjamin Petersond71ca412008-05-08 23:44:58 +000017 "ismount", "expanduser","expandvars","normpath","abspath",
Georg Brandlf0de6a12005-08-22 18:02:59 +000018 "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
Brian Curtind40e6f72010-07-08 21:39:08 +000019 "extsep","devnull","realpath","supports_unicode_filenames","relpath",
Brian Curtin62857742010-09-06 17:07:27 +000020 "samefile", "sameopenfile",]
Guido van Rossum555915a1994-02-24 11:32:59 +000021
Skip Montanaro117910d2003-02-14 19:35:31 +000022# strings representing various path-related bits and pieces
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000023# These are primarily for export; internally, they are hardcoded.
Skip Montanaro117910d2003-02-14 19:35:31 +000024curdir = '.'
25pardir = '..'
26extsep = '.'
27sep = '\\'
28pathsep = ';'
Skip Montanaro9ddac3e2003-03-28 22:23:24 +000029altsep = '/'
Andrew MacIntyre437966c2003-02-17 09:17:50 +000030defpath = '.;C:\\bin'
Skip Montanaro117910d2003-02-14 19:35:31 +000031if 'ce' in sys.builtin_module_names:
32 defpath = '\\Windows'
33elif 'os2' in sys.builtin_module_names:
Andrew MacIntyre437966c2003-02-17 09:17:50 +000034 # OS/2 w/ VACPP
Skip Montanaro117910d2003-02-14 19:35:31 +000035 altsep = '/'
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000036devnull = 'nul'
Skip Montanaro117910d2003-02-14 19:35:31 +000037
Mark Hammond5a607a32009-05-06 08:04:54 +000038def _get_empty(path):
39 if isinstance(path, bytes):
40 return b''
41 else:
42 return ''
43
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000044def _get_sep(path):
45 if isinstance(path, bytes):
46 return b'\\'
47 else:
48 return '\\'
49
50def _get_altsep(path):
51 if isinstance(path, bytes):
52 return b'/'
53 else:
54 return '/'
55
56def _get_bothseps(path):
57 if isinstance(path, bytes):
58 return b'\\/'
59 else:
60 return '\\/'
61
62def _get_dot(path):
63 if isinstance(path, bytes):
64 return b'.'
65 else:
66 return '.'
67
68def _get_colon(path):
69 if isinstance(path, bytes):
70 return b':'
71 else:
72 return ':'
73
Georg Brandlcfb68212010-07-31 21:40:15 +000074def _get_special(path):
75 if isinstance(path, bytes):
76 return (b'\\\\.\\', b'\\\\?\\')
77 else:
78 return ('\\\\.\\', '\\\\?\\')
79
Guido van Rossume2ad88c1997-08-12 14:46:58 +000080# Normalize the case of a pathname and map slashes to backslashes.
81# Other normalizations (such as optimizing '../' away) are not done
Guido van Rossum555915a1994-02-24 11:32:59 +000082# (this is done by normpath).
Guido van Rossume2ad88c1997-08-12 14:46:58 +000083
Guido van Rossum555915a1994-02-24 11:32:59 +000084def normcase(s):
Guido van Rossum16a0bc21998-02-18 13:48:31 +000085 """Normalize case of pathname.
86
Guido van Rossum534972b1999-02-03 17:20:50 +000087 Makes all characters lowercase and all slashes into backslashes."""
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +000088 if not isinstance(s, (bytes, str)):
89 raise TypeError("normcase() argument must be str or bytes, "
90 "not '{}'".format(s.__class__.__name__))
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +000091 return s.replace(_get_altsep(s), _get_sep(s)).lower()
Guido van Rossum555915a1994-02-24 11:32:59 +000092
Guido van Rossum77e1db31997-06-02 23:11:57 +000093
Fred Drakeef0b5dd2000-02-17 17:30:40 +000094# Return whether a path is absolute.
Mark Hammond5a607a32009-05-06 08:04:54 +000095# Trivial in Posix, harder on Windows.
96# For Windows it is absolute if it starts with a slash or backslash (current
97# volume), or if a pathname after the volume-letter-and-colon or UNC-resource
Guido van Rossum534972b1999-02-03 17:20:50 +000098# starts with a slash or backslash.
Guido van Rossum555915a1994-02-24 11:32:59 +000099
100def isabs(s):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000101 """Test whether a path is absolute"""
102 s = splitdrive(s)[1]
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000103 return len(s) > 0 and s[:1] in _get_bothseps(s)
Guido van Rossum555915a1994-02-24 11:32:59 +0000104
105
Guido van Rossum77e1db31997-06-02 23:11:57 +0000106# Join two (or more) paths.
107
Barry Warsaw384d2491997-02-18 21:53:25 +0000108def join(a, *p):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000109 """Join two or more pathname components, inserting "\\" as needed.
110 If any component is an absolute path, all previous path components
111 will be discarded."""
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000112 sep = _get_sep(a)
113 seps = _get_bothseps(a)
114 colon = _get_colon(a)
Guido van Rossum15e22e11997-12-05 19:03:01 +0000115 path = a
116 for b in p:
Tim Peters33dc0a12001-07-27 08:09:54 +0000117 b_wins = 0 # set to 1 iff b makes path irrelevant
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000118 if not path:
Tim Peters33dc0a12001-07-27 08:09:54 +0000119 b_wins = 1
Tim Peters1bdd0f22001-07-19 17:18:18 +0000120
Tim Peters33dc0a12001-07-27 08:09:54 +0000121 elif isabs(b):
122 # This probably wipes out path so far. However, it's more
Mark Hammond5a607a32009-05-06 08:04:54 +0000123 # complicated if path begins with a drive letter. You get a+b
124 # (minus redundant slashes) in these four cases:
Tim Peters33dc0a12001-07-27 08:09:54 +0000125 # 1. join('c:', '/a') == 'c:/a'
Mark Hammond5a607a32009-05-06 08:04:54 +0000126 # 2. join('//computer/share', '/a') == '//computer/share/a'
127 # 3. join('c:/', '/a') == 'c:/a'
128 # 4. join('//computer/share/', '/a') == '//computer/share/a'
129 # But b wins in all of these cases:
130 # 5. join('c:/a', '/b') == '/b'
131 # 6. join('//computer/share/a', '/b') == '/b'
132 # 7. join('c:', 'd:/') == 'd:/'
133 # 8. join('c:', '//computer/share/') == '//computer/share/'
134 # 9. join('//computer/share', 'd:/') == 'd:/'
135 # 10. join('//computer/share', '//computer/share/') == '//computer/share/'
136 # 11. join('c:/', 'd:/') == 'd:/'
137 # 12. join('c:/', '//computer/share/') == '//computer/share/'
138 # 13. join('//computer/share/', 'd:/') == 'd:/'
139 # 14. join('//computer/share/', '//computer/share/') == '//computer/share/'
140 b_prefix, b_rest = splitdrive(b)
Tim Peters1bdd0f22001-07-19 17:18:18 +0000141
Mark Hammond5a607a32009-05-06 08:04:54 +0000142 # if b has a prefix, it always wins.
143 if b_prefix:
Tim Peters33dc0a12001-07-27 08:09:54 +0000144 b_wins = 1
Mark Hammond5a607a32009-05-06 08:04:54 +0000145 else:
146 # b doesn't have a prefix.
147 # but isabs(b) returned true.
148 # and therefore b_rest[0] must be a slash.
149 # (but let's check that.)
150 assert(b_rest and b_rest[0] in seps)
151
152 # so, b still wins if path has a rest that's more than a sep.
153 # you get a+b if path_rest is empty or only has a sep.
154 # (see cases 1-4 for times when b loses.)
155 path_rest = splitdrive(path)[1]
156 b_wins = path_rest and path_rest not in seps
Tim Peters1bdd0f22001-07-19 17:18:18 +0000157
Tim Peters33dc0a12001-07-27 08:09:54 +0000158 if b_wins:
159 path = b
160 else:
161 # Join, and ensure there's a separator.
162 assert len(path) > 0
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000163 if path[-1:] in seps:
164 if b and b[:1] in seps:
Tim Peters33dc0a12001-07-27 08:09:54 +0000165 path += b[1:]
166 else:
167 path += b
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000168 elif path[-1:] == colon:
Tim Peters33dc0a12001-07-27 08:09:54 +0000169 path += b
170 elif b:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000171 if b[:1] in seps:
Tim Peters33dc0a12001-07-27 08:09:54 +0000172 path += b
173 else:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000174 path += sep + b
Tim Peters6a3e5f12001-11-05 21:25:02 +0000175 else:
176 # path is not empty and does not end with a backslash,
177 # but b is empty; since, e.g., split('a/') produces
178 # ('a', ''), it's best if join() adds a backslash in
179 # this case.
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000180 path += sep
Tim Peters1bdd0f22001-07-19 17:18:18 +0000181
Guido van Rossum15e22e11997-12-05 19:03:01 +0000182 return path
Guido van Rossum555915a1994-02-24 11:32:59 +0000183
184
185# Split a path in a drive specification (a drive letter followed by a
Guido van Rossumf3c695c1999-04-06 19:32:19 +0000186# colon) and the path specification.
Guido van Rossum555915a1994-02-24 11:32:59 +0000187# It is always true that drivespec + pathspec == p
188def splitdrive(p):
Mark Hammond5a607a32009-05-06 08:04:54 +0000189 """Split a pathname into drive/UNC sharepoint and relative path specifiers.
190 Returns a 2-tuple (drive_or_unc, path); either part may be empty.
191
192 If you assign
193 result = splitdrive(p)
194 It is always true that:
195 result[0] + result[1] == p
196
197 If the path contained a drive letter, drive_or_unc will contain everything
198 up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir")
199
200 If the path contained a UNC path, the drive_or_unc will contain the host name
201 and share up to but not including the fourth directory separator character.
202 e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir")
203
204 Paths cannot contain both a drive letter and a UNC path.
205
206 """
207 empty = _get_empty(p)
208 if len(p) > 1:
209 sep = _get_sep(p)
210 normp = normcase(p)
211 if (normp[0:2] == sep*2) and (normp[2:3] != sep):
212 # is a UNC path:
213 # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
214 # \\machine\mountpoint\directory\etc\...
215 # directory ^^^^^^^^^^^^^^^
216 index = normp.find(sep, 2)
217 if index == -1:
218 return empty, p
219 index2 = normp.find(sep, index + 1)
220 # a UNC path can't have two slashes in a row
221 # (after the initial two)
222 if index2 == index + 1:
223 return empty, p
224 if index2 == -1:
225 index2 = len(p)
226 return p[:index2], p[index2:]
227 if normp[1:2] == _get_colon(p):
228 return p[:2], p[2:]
229 return empty, p
Guido van Rossumf3c695c1999-04-06 19:32:19 +0000230
231
232# Parse UNC paths
233def splitunc(p):
Mark Hammond5a607a32009-05-06 08:04:54 +0000234 """Deprecated since Python 3.1. Please use splitdrive() instead;
235 it now handles UNC paths.
236
237 Split a pathname into UNC mount point and relative path specifiers.
Guido van Rossumf3c695c1999-04-06 19:32:19 +0000238
239 Return a 2-tuple (unc, rest); either part may be empty.
240 If unc is not empty, it has the form '//host/mount' (or similar
241 using backslashes). unc+rest is always the input path.
242 Paths containing drive letters never have an UNC part.
243 """
Mark Hammond5a607a32009-05-06 08:04:54 +0000244 import warnings
245 warnings.warn("ntpath.splitunc is deprecated, use ntpath.splitdrive instead",
Gregory P. Smithaa3b5b82009-06-30 05:33:50 +0000246 DeprecationWarning)
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000247 sep = _get_sep(p)
248 if not p[1:2]:
249 return p[:0], p # Drive letter present
Guido van Rossum534972b1999-02-03 17:20:50 +0000250 firstTwo = p[0:2]
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000251 if normcase(firstTwo) == sep + sep:
Guido van Rossum534972b1999-02-03 17:20:50 +0000252 # is a UNC path:
253 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
254 # \\machine\mountpoint\directories...
255 # directory ^^^^^^^^^^^^^^^
256 normp = normcase(p)
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000257 index = normp.find(sep, 2)
Guido van Rossum534972b1999-02-03 17:20:50 +0000258 if index == -1:
259 ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000260 return (p[:0], p)
261 index = normp.find(sep, index + 1)
Guido van Rossum534972b1999-02-03 17:20:50 +0000262 if index == -1:
263 index = len(p)
264 return p[:index], p[index:]
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000265 return p[:0], p
Guido van Rossum555915a1994-02-24 11:32:59 +0000266
267
268# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000269# rest). After the trailing '/' is stripped, the invariant
Guido van Rossum555915a1994-02-24 11:32:59 +0000270# join(head, tail) == p holds.
271# The resulting head won't end in '/' unless it is the root.
272
273def split(p):
Guido van Rossum534972b1999-02-03 17:20:50 +0000274 """Split a pathname.
275
276 Return tuple (head, tail) where tail is everything after the final slash.
277 Either part may be empty."""
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000278
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000279 seps = _get_bothseps(p)
Guido van Rossum15e22e11997-12-05 19:03:01 +0000280 d, p = splitdrive(p)
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000281 # set i to index beyond p's last slash
282 i = len(p)
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000283 while i and p[i-1] not in seps:
Georg Brandl599b65d2010-07-23 08:46:35 +0000284 i -= 1
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000285 head, tail = p[:i], p[i:] # now tail has no slashes
286 # remove trailing slashes from head, unless it's all slashes
287 head2 = head
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000288 while head2 and head2[-1:] in seps:
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000289 head2 = head2[:-1]
290 head = head2 or head
Guido van Rossum15e22e11997-12-05 19:03:01 +0000291 return d + head, tail
Guido van Rossum555915a1994-02-24 11:32:59 +0000292
293
294# Split a path in root and extension.
Guido van Rossum73e122f1997-01-22 00:17:26 +0000295# The extension is everything starting at the last dot in the last
Guido van Rossum555915a1994-02-24 11:32:59 +0000296# pathname component; the root is everything before that.
297# It is always true that root + ext == p.
298
299def splitext(p):
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000300 return genericpath._splitext(p, _get_sep(p), _get_altsep(p),
301 _get_dot(p))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000302splitext.__doc__ = genericpath._splitext.__doc__
Guido van Rossum555915a1994-02-24 11:32:59 +0000303
304
305# Return the tail (basename) part of a path.
306
307def basename(p):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000308 """Returns the final component of a pathname"""
309 return split(p)[1]
Guido van Rossum555915a1994-02-24 11:32:59 +0000310
311
312# Return the head (dirname) part of a path.
313
314def dirname(p):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000315 """Returns the directory component of a pathname"""
316 return split(p)[0]
Guido van Rossum555915a1994-02-24 11:32:59 +0000317
Guido van Rossum555915a1994-02-24 11:32:59 +0000318# Is a path a symbolic link?
Brian Curtind40e6f72010-07-08 21:39:08 +0000319# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum555915a1994-02-24 11:32:59 +0000320
321def islink(path):
Brian Curtind40e6f72010-07-08 21:39:08 +0000322 """Test whether a path is a symbolic link.
323 This will always return false for Windows prior to 6.0
324 and for OS/2.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000325 """
Brian Curtind40e6f72010-07-08 21:39:08 +0000326 try:
327 st = os.lstat(path)
328 except (os.error, AttributeError):
329 return False
330 return stat.S_ISLNK(st.st_mode)
Guido van Rossum555915a1994-02-24 11:32:59 +0000331
Brian Curtind40e6f72010-07-08 21:39:08 +0000332# Being true for dangling symbolic links is also useful.
333
334def lexists(path):
335 """Test whether a path exists. Returns True for broken symbolic links"""
336 try:
337 st = os.lstat(path)
338 except (os.error, WindowsError):
339 return False
340 return True
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000341
Guido van Rossumf3c695c1999-04-06 19:32:19 +0000342# Is a path a mount point? Either a root (with or without drive letter)
343# or an UNC path with at most a / or \ after the mount point.
Guido van Rossum555915a1994-02-24 11:32:59 +0000344
345def ismount(path):
Guido van Rossumca99c2c1998-01-19 22:25:59 +0000346 """Test whether a path is a mount point (defined as root of drive)"""
Benjamin Peterson48e24782009-03-29 13:02:52 +0000347 seps = _get_bothseps(path)
Mark Hammond5a607a32009-05-06 08:04:54 +0000348 root, rest = splitdrive(path)
349 if root and root[0] in seps:
350 return (not rest) or (rest in seps)
351 return rest in seps
Guido van Rossum555915a1994-02-24 11:32:59 +0000352
353
Guido van Rossum555915a1994-02-24 11:32:59 +0000354# Expand paths beginning with '~' or '~user'.
355# '~' means $HOME; '~user' means that user's home directory.
356# If the path doesn't begin with '~', or if the user or $HOME is unknown,
357# the path is returned unchanged (leaving error reporting to whatever
358# function is called with the expanded path as argument).
359# See also module 'glob' for expansion of *, ? and [...] in pathnames.
360# (A function should also be defined to do full *sh-style environment
361# variable expansion.)
362
363def expanduser(path):
Guido van Rossum534972b1999-02-03 17:20:50 +0000364 """Expand ~ and ~user constructs.
365
366 If user or $HOME is unknown, do nothing."""
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000367 if isinstance(path, bytes):
368 tilde = b'~'
369 else:
370 tilde = '~'
371 if not path.startswith(tilde):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000372 return path
373 i, n = 1, len(path)
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000374 while i < n and path[i] not in _get_bothseps(path):
Georg Brandl599b65d2010-07-23 08:46:35 +0000375 i += 1
Guido van Rossumd8faa362007-04-27 19:54:29 +0000376
377 if 'HOME' in os.environ:
378 userhome = os.environ['HOME']
379 elif 'USERPROFILE' in os.environ:
380 userhome = os.environ['USERPROFILE']
381 elif not 'HOMEPATH' in os.environ:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000382 return path
Guido van Rossumd8faa362007-04-27 19:54:29 +0000383 else:
384 try:
385 drive = os.environ['HOMEDRIVE']
386 except KeyError:
387 drive = ''
388 userhome = join(drive, os.environ['HOMEPATH'])
389
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000390 if isinstance(path, bytes):
391 userhome = userhome.encode(sys.getfilesystemencoding())
392
Guido van Rossumd8faa362007-04-27 19:54:29 +0000393 if i != 1: #~user
394 userhome = join(dirname(userhome), path[1:i])
395
Guido van Rossum15e22e11997-12-05 19:03:01 +0000396 return userhome + path[i:]
Guido van Rossum555915a1994-02-24 11:32:59 +0000397
398
399# Expand paths containing shell variable substitutions.
400# The following rules apply:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000401# - no expansion within single quotes
Guido van Rossumd8faa362007-04-27 19:54:29 +0000402# - '$$' is translated into '$'
403# - '%%' is translated into '%' if '%%' are not seen in %var1%%var2%
Guido van Rossum15e22e11997-12-05 19:03:01 +0000404# - ${varname} is accepted.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000405# - $varname is accepted.
406# - %varname% is accepted.
407# - varnames can be made out of letters, digits and the characters '_-'
408# (though is not verifed in the ${varname} and %varname% cases)
Guido van Rossum555915a1994-02-24 11:32:59 +0000409# XXX With COMMAND.COM you can use any characters in a variable name,
410# XXX except '^|<>='.
411
Tim Peters2344fae2001-01-15 00:50:52 +0000412def expandvars(path):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000413 """Expand shell variables of the forms $var, ${var} and %var%.
Guido van Rossum534972b1999-02-03 17:20:50 +0000414
415 Unknown variables are left unchanged."""
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000416 if isinstance(path, bytes):
417 if ord('$') not in path and ord('%') not in path:
418 return path
419 import string
420 varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000421 quote = b'\''
422 percent = b'%'
423 brace = b'{'
424 dollar = b'$'
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000425 else:
426 if '$' not in path and '%' not in path:
427 return path
428 import string
429 varchars = string.ascii_letters + string.digits + '_-'
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000430 quote = '\''
431 percent = '%'
432 brace = '{'
433 dollar = '$'
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000434 res = path[:0]
Guido van Rossum15e22e11997-12-05 19:03:01 +0000435 index = 0
436 pathlen = len(path)
437 while index < pathlen:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000438 c = path[index:index+1]
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000439 if c == quote: # no expansion within single quotes
Guido van Rossum15e22e11997-12-05 19:03:01 +0000440 path = path[index + 1:]
441 pathlen = len(path)
442 try:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000443 index = path.index(c)
Georg Brandl599b65d2010-07-23 08:46:35 +0000444 res += c + path[:index + 1]
Fred Drakeb4e460a2000-09-28 16:25:20 +0000445 except ValueError:
Georg Brandl599b65d2010-07-23 08:46:35 +0000446 res += path
Fred Drakeb4e460a2000-09-28 16:25:20 +0000447 index = pathlen - 1
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000448 elif c == percent: # variable or '%'
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000449 if path[index + 1:index + 2] == percent:
Georg Brandl599b65d2010-07-23 08:46:35 +0000450 res += c
451 index += 1
Guido van Rossumd8faa362007-04-27 19:54:29 +0000452 else:
453 path = path[index+1:]
454 pathlen = len(path)
455 try:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000456 index = path.index(percent)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000457 except ValueError:
Georg Brandl599b65d2010-07-23 08:46:35 +0000458 res += percent + path
Guido van Rossumd8faa362007-04-27 19:54:29 +0000459 index = pathlen - 1
460 else:
461 var = path[:index]
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000462 if isinstance(path, bytes):
463 var = var.decode('ascii')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000464 if var in os.environ:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000465 value = os.environ[var]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000466 else:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000467 value = '%' + var + '%'
468 if isinstance(path, bytes):
469 value = value.encode('ascii')
Georg Brandl599b65d2010-07-23 08:46:35 +0000470 res += value
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000471 elif c == dollar: # variable or '$$'
472 if path[index + 1:index + 2] == dollar:
Georg Brandl599b65d2010-07-23 08:46:35 +0000473 res += c
474 index += 1
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000475 elif path[index + 1:index + 2] == brace:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000476 path = path[index+2:]
477 pathlen = len(path)
478 try:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000479 if isinstance(path, bytes):
480 index = path.index(b'}')
Thomas Woutersb2137042007-02-01 18:02:27 +0000481 else:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000482 index = path.index('}')
483 var = path[:index]
484 if isinstance(path, bytes):
485 var = var.decode('ascii')
486 if var in os.environ:
487 value = os.environ[var]
488 else:
489 value = '${' + var + '}'
490 if isinstance(path, bytes):
491 value = value.encode('ascii')
Georg Brandl599b65d2010-07-23 08:46:35 +0000492 res += value
Fred Drakeb4e460a2000-09-28 16:25:20 +0000493 except ValueError:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000494 if isinstance(path, bytes):
Georg Brandl599b65d2010-07-23 08:46:35 +0000495 res += b'${' + path
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000496 else:
Georg Brandl599b65d2010-07-23 08:46:35 +0000497 res += '${' + path
Guido van Rossum15e22e11997-12-05 19:03:01 +0000498 index = pathlen - 1
499 else:
500 var = ''
Georg Brandl599b65d2010-07-23 08:46:35 +0000501 index += 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000502 c = path[index:index + 1]
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000503 while c and c in varchars:
504 if isinstance(path, bytes):
Georg Brandl599b65d2010-07-23 08:46:35 +0000505 var += c.decode('ascii')
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000506 else:
Georg Brandl599b65d2010-07-23 08:46:35 +0000507 var += c
508 index += 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000509 c = path[index:index + 1]
Raymond Hettinger54f02222002-06-01 14:18:47 +0000510 if var in os.environ:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000511 value = os.environ[var]
Thomas Woutersb2137042007-02-01 18:02:27 +0000512 else:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000513 value = '$' + var
514 if isinstance(path, bytes):
515 value = value.encode('ascii')
Georg Brandl599b65d2010-07-23 08:46:35 +0000516 res += value
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000517 if c:
Georg Brandl599b65d2010-07-23 08:46:35 +0000518 index -= 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000519 else:
Georg Brandl599b65d2010-07-23 08:46:35 +0000520 res += c
521 index += 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000522 return res
Guido van Rossum555915a1994-02-24 11:32:59 +0000523
524
Tim Peters54a14a32001-08-30 22:05:26 +0000525# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
Guido van Rossum3df7b5a1996-08-26 16:35:26 +0000526# Previously, this function also truncated pathnames to 8+3 format,
527# but as this module is called "ntpath", that's obviously wrong!
Guido van Rossum555915a1994-02-24 11:32:59 +0000528
529def normpath(path):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000530 """Normalize path, eliminating double slashes, etc."""
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000531 sep = _get_sep(path)
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000532 dotdot = _get_dot(path) * 2
Georg Brandlcfb68212010-07-31 21:40:15 +0000533 special_prefixes = _get_special(path)
534 if path.startswith(special_prefixes):
535 # in the case of paths with these prefixes:
536 # \\.\ -> device names
537 # \\?\ -> literal paths
538 # do not do any normalization, but return the path unchanged
539 return path
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000540 path = path.replace(_get_altsep(path), sep)
Guido van Rossum15e22e11997-12-05 19:03:01 +0000541 prefix, path = splitdrive(path)
Mark Hammond5a607a32009-05-06 08:04:54 +0000542
543 # collapse initial backslashes
544 if path.startswith(sep):
Georg Brandl599b65d2010-07-23 08:46:35 +0000545 prefix += sep
Mark Hammond5a607a32009-05-06 08:04:54 +0000546 path = path.lstrip(sep)
547
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000548 comps = path.split(sep)
Guido van Rossum15e22e11997-12-05 19:03:01 +0000549 i = 0
550 while i < len(comps):
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000551 if not comps[i] or comps[i] == _get_dot(path):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000552 del comps[i]
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000553 elif comps[i] == dotdot:
554 if i > 0 and comps[i-1] != dotdot:
Tim Peters54a14a32001-08-30 22:05:26 +0000555 del comps[i-1:i+1]
556 i -= 1
Amaury Forgeot d'Arc3b44e612008-10-03 20:32:33 +0000557 elif i == 0 and prefix.endswith(_get_sep(path)):
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000558 del comps[i]
559 else:
560 i += 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000561 else:
Tim Peters54a14a32001-08-30 22:05:26 +0000562 i += 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000563 # If the path is now empty, substitute '.'
564 if not prefix and not comps:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000565 comps.append(_get_dot(path))
566 return prefix + sep.join(comps)
Guido van Rossume294cf61999-01-29 18:05:18 +0000567
568
569# Return an absolute path.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000570try:
571 from nt import _getfullpathname
Mark Hammondf717f052002-01-17 00:44:26 +0000572
Thomas Wouters477c8d52006-05-27 19:21:47 +0000573except ImportError: # not running on Windows - mock up something sensible
574 def abspath(path):
575 """Return the absolute version of a path."""
576 if not isabs(path):
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000577 if isinstance(path, bytes):
578 cwd = os.getcwdb()
579 else:
580 cwd = os.getcwd()
581 path = join(cwd, path)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000582 return normpath(path)
583
584else: # use native Windows method on Windows
585 def abspath(path):
586 """Return the absolute version of a path."""
587
588 if path: # Empty path must return current working directory.
589 try:
590 path = _getfullpathname(path)
591 except WindowsError:
592 pass # Bad path - return unchanged.
Florent Xiclunaad8c5ca2010-03-08 14:44:41 +0000593 elif isinstance(path, bytes):
594 path = os.getcwdb()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000595 else:
596 path = os.getcwd()
597 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000598
599# realpath is a no-op on systems without islink support
600realpath = abspath
Mark Hammond8696ebc2002-10-08 02:44:31 +0000601# Win9x family and earlier have no Unicode filename support.
Tim Peters26bc25a2002-10-09 07:56:04 +0000602supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
603 sys.getwindowsversion()[3] >= 2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000604
605def relpath(path, start=curdir):
606 """Return a relative version of a path"""
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000607 sep = _get_sep(path)
608
609 if start is curdir:
610 start = _get_dot(path)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000611
612 if not path:
613 raise ValueError("no path specified")
Mark Hammond5a607a32009-05-06 08:04:54 +0000614
615 start_abs = abspath(normpath(start))
616 path_abs = abspath(normpath(path))
617 start_drive, start_rest = splitdrive(start_abs)
618 path_drive, path_rest = splitdrive(path_abs)
Hirokazu Yamamotob08820a2010-10-18 12:13:18 +0000619 if normcase(start_drive) != normcase(path_drive):
Mark Hammond5a607a32009-05-06 08:04:54 +0000620 error = "path is on mount '{0}', start on mount '{1}'".format(
621 path_drive, start_drive)
622 raise ValueError(error)
623
624 start_list = [x for x in start_rest.split(sep) if x]
625 path_list = [x for x in path_rest.split(sep) if x]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000626 # Work out how much of the filepath is shared by start and path.
Mark Hammond5a607a32009-05-06 08:04:54 +0000627 i = 0
628 for e1, e2 in zip(start_list, path_list):
Hirokazu Yamamotob08820a2010-10-18 12:13:18 +0000629 if normcase(e1) != normcase(e2):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000630 break
Guido van Rossumd8faa362007-04-27 19:54:29 +0000631 i += 1
632
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000633 if isinstance(path, bytes):
634 pardir = b'..'
635 else:
636 pardir = '..'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000637 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
Christian Heimesfaf2f632008-01-06 16:59:19 +0000638 if not rel_list:
Amaury Forgeot d'Arcc72ef8b2008-10-03 18:38:26 +0000639 return _get_dot(path)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000640 return join(*rel_list)
Brian Curtind40e6f72010-07-08 21:39:08 +0000641
642
643# determine if two files are in fact the same file
Brian Curtin0dac8082010-09-23 20:38:14 +0000644try:
Brian Curtine8e80422010-09-24 13:56:34 +0000645 # GetFinalPathNameByHandle is available starting with Windows 6.0.
646 # Windows XP and non-Windows OS'es will mock _getfinalpathname.
647 if sys.getwindowsversion()[:2] >= (6, 0):
648 from nt import _getfinalpathname
649 else:
650 raise ImportError
651except (AttributeError, ImportError):
Brian Curtin0dac8082010-09-23 20:38:14 +0000652 # On Windows XP and earlier, two files are the same if their absolute
653 # pathnames are the same.
Brian Curtine8e80422010-09-24 13:56:34 +0000654 # Non-Windows operating systems fake this method with an XP
655 # approximation.
Brian Curtin0dac8082010-09-23 20:38:14 +0000656 def _getfinalpathname(f):
657 return abspath(f)
658
Brian Curtind40e6f72010-07-08 21:39:08 +0000659def samefile(f1, f2):
660 "Test whether two pathnames reference the same actual file"
Brian Curtin0dac8082010-09-23 20:38:14 +0000661 return _getfinalpathname(f1) == _getfinalpathname(f2)
662
663
664try:
665 from nt import _getfileinformation
666except ImportError:
667 # On other operating systems, just return the fd and see that
668 # it compares equal in sameopenfile.
669 def _getfileinformation(fd):
670 return fd
Brian Curtin62857742010-09-06 17:07:27 +0000671
672def sameopenfile(f1, f2):
673 """Test whether two file objects reference the same file"""
Brian Curtin0dac8082010-09-23 20:38:14 +0000674 return _getfileinformation(f1) == _getfileinformation(f2)