blob: fc9463c48e4f399d96d5aa4248538571ccb5b6fd [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 Heimesc5f05e42008-02-23 17:40:11 +000010import stat
Martin v. Löwis05c075d2007-03-07 11:04:33 +000011import genericpath
Benjamin Peterson0893a0a2008-05-09 00:27:01 +000012import warnings
13
Jack Diederich7b604642006-08-26 18:42:06 +000014from genericpath import *
Skip Montanaro4d5d5bf2000-07-13 01:01:03 +000015
Skip Montanaro269b83b2001-02-06 01:07:02 +000016__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
17 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000018 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
19 "ismount","walk","expanduser","expandvars","normpath","abspath",
20 "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
Collin Winter6f187742007-03-16 22:16:08 +000021 "extsep","devnull","realpath","supports_unicode_filenames","relpath"]
Guido van Rossum555915a1994-02-24 11:32:59 +000022
Skip Montanaro117910d2003-02-14 19:35:31 +000023# strings representing various path-related bits and pieces
24curdir = '.'
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
Guido van Rossume2ad88c1997-08-12 14:46:58 +000038# Normalize the case of a pathname and map slashes to backslashes.
39# Other normalizations (such as optimizing '../' away) are not done
Guido van Rossum555915a1994-02-24 11:32:59 +000040# (this is done by normpath).
Guido van Rossume2ad88c1997-08-12 14:46:58 +000041
Guido van Rossum555915a1994-02-24 11:32:59 +000042def normcase(s):
Guido van Rossum16a0bc21998-02-18 13:48:31 +000043 """Normalize case of pathname.
44
Guido van Rossum534972b1999-02-03 17:20:50 +000045 Makes all characters lowercase and all slashes into backslashes."""
Fred Drakeb4e460a2000-09-28 16:25:20 +000046 return s.replace("/", "\\").lower()
Guido van Rossum555915a1994-02-24 11:32:59 +000047
Guido van Rossum77e1db31997-06-02 23:11:57 +000048
Fred Drakeef0b5dd2000-02-17 17:30:40 +000049# Return whether a path is absolute.
Guido van Rossum555915a1994-02-24 11:32:59 +000050# Trivial in Posix, harder on the Mac or MS-DOS.
51# For DOS it is absolute if it starts with a slash or backslash (current
Guido van Rossum534972b1999-02-03 17:20:50 +000052# volume), or if a pathname after the volume letter and colon / UNC resource
53# starts with a slash or backslash.
Guido van Rossum555915a1994-02-24 11:32:59 +000054
55def isabs(s):
Guido van Rossum15e22e11997-12-05 19:03:01 +000056 """Test whether a path is absolute"""
57 s = splitdrive(s)[1]
58 return s != '' and s[:1] in '/\\'
Guido van Rossum555915a1994-02-24 11:32:59 +000059
60
Guido van Rossum77e1db31997-06-02 23:11:57 +000061# Join two (or more) paths.
Serhiy Storchaka31f51212014-01-27 23:14:51 +020062def join(path, *paths):
63 """Join two or more pathname components, inserting "\\" as needed."""
64 result_drive, result_path = splitdrive(path)
65 for p in paths:
66 p_drive, p_path = splitdrive(p)
67 if p_path and p_path[0] in '\\/':
68 # Second path is absolute
69 if p_drive or not result_drive:
70 result_drive = p_drive
71 result_path = p_path
72 continue
73 elif p_drive and p_drive != result_drive:
74 if p_drive.lower() != result_drive.lower():
75 # Different drives => ignore the first path entirely
76 result_drive = p_drive
77 result_path = p_path
78 continue
79 # Same drive in different case
80 result_drive = p_drive
81 # Second path is relative to the first
82 if result_path and result_path[-1] not in '\\/':
83 result_path = result_path + '\\'
84 result_path = result_path + p_path
85 return result_drive + result_path
Guido van Rossum555915a1994-02-24 11:32:59 +000086
87
88# Split a path in a drive specification (a drive letter followed by a
Guido van Rossumf3c695c1999-04-06 19:32:19 +000089# colon) and the path specification.
Guido van Rossum555915a1994-02-24 11:32:59 +000090# It is always true that drivespec + pathspec == p
91def splitdrive(p):
Guido van Rossumf3c695c1999-04-06 19:32:19 +000092 """Split a pathname into drive and path specifiers. Returns a 2-tuple
93"(drive,path)"; either part may be empty"""
Guido van Rossum15e22e11997-12-05 19:03:01 +000094 if p[1:2] == ':':
95 return p[0:2], p[2:]
Guido van Rossumf3c695c1999-04-06 19:32:19 +000096 return '', p
97
98
99# Parse UNC paths
100def splitunc(p):
101 """Split a pathname into UNC mount point and relative path specifiers.
102
103 Return a 2-tuple (unc, rest); either part may be empty.
104 If unc is not empty, it has the form '//host/mount' (or similar
105 using backslashes). unc+rest is always the input path.
106 Paths containing drive letters never have an UNC part.
107 """
108 if p[1:2] == ':':
109 return '', p # Drive letter present
Guido van Rossum534972b1999-02-03 17:20:50 +0000110 firstTwo = p[0:2]
111 if firstTwo == '//' or firstTwo == '\\\\':
112 # is a UNC path:
113 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
114 # \\machine\mountpoint\directories...
115 # directory ^^^^^^^^^^^^^^^
Serhiy Storchakadd5a46c2013-12-16 15:15:29 +0200116 normp = p.replace('\\', '/')
117 index = normp.find('/', 2)
118 if index <= 2:
119 return '', p
120 index2 = normp.find('/', index + 1)
121 # a UNC path can't have two slashes in a row
122 # (after the initial two)
123 if index2 == index + 1:
124 return '', p
125 if index2 == -1:
126 index2 = len(p)
127 return p[:index2], p[index2:]
Guido van Rossum15e22e11997-12-05 19:03:01 +0000128 return '', p
Guido van Rossum555915a1994-02-24 11:32:59 +0000129
130
131# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000132# rest). After the trailing '/' is stripped, the invariant
Guido van Rossum555915a1994-02-24 11:32:59 +0000133# join(head, tail) == p holds.
134# The resulting head won't end in '/' unless it is the root.
135
136def split(p):
Guido van Rossum534972b1999-02-03 17:20:50 +0000137 """Split a pathname.
138
139 Return tuple (head, tail) where tail is everything after the final slash.
140 Either part may be empty."""
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000141
Guido van Rossum15e22e11997-12-05 19:03:01 +0000142 d, p = splitdrive(p)
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000143 # set i to index beyond p's last slash
144 i = len(p)
145 while i and p[i-1] not in '/\\':
146 i = i - 1
147 head, tail = p[:i], p[i:] # now tail has no slashes
148 # remove trailing slashes from head, unless it's all slashes
149 head2 = head
150 while head2 and head2[-1] in '/\\':
151 head2 = head2[:-1]
152 head = head2 or head
Guido van Rossum15e22e11997-12-05 19:03:01 +0000153 return d + head, tail
Guido van Rossum555915a1994-02-24 11:32:59 +0000154
155
156# Split a path in root and extension.
Guido van Rossum73e122f1997-01-22 00:17:26 +0000157# The extension is everything starting at the last dot in the last
Guido van Rossum555915a1994-02-24 11:32:59 +0000158# pathname component; the root is everything before that.
159# It is always true that root + ext == p.
160
161def splitext(p):
Martin v. Löwis05c075d2007-03-07 11:04:33 +0000162 return genericpath._splitext(p, sep, altsep, extsep)
163splitext.__doc__ = genericpath._splitext.__doc__
Guido van Rossum555915a1994-02-24 11:32:59 +0000164
165
166# Return the tail (basename) part of a path.
167
168def basename(p):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000169 """Returns the final component of a pathname"""
170 return split(p)[1]
Guido van Rossum555915a1994-02-24 11:32:59 +0000171
172
173# Return the head (dirname) part of a path.
174
175def dirname(p):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000176 """Returns the directory component of a pathname"""
177 return split(p)[0]
Guido van Rossum555915a1994-02-24 11:32:59 +0000178
Guido van Rossum555915a1994-02-24 11:32:59 +0000179# Is a path a symbolic link?
180# This will always return false on systems where posix.lstat doesn't exist.
181
182def islink(path):
Jack Diederich7b604642006-08-26 18:42:06 +0000183 """Test for symbolic link.
184 On WindowsNT/95 and OS/2 always returns false
185 """
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000186 return False
Guido van Rossum555915a1994-02-24 11:32:59 +0000187
Jack Diederich7b604642006-08-26 18:42:06 +0000188# alias exists to lexists
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000189lexists = exists
190
Guido van Rossumf3c695c1999-04-06 19:32:19 +0000191# Is a path a mount point? Either a root (with or without drive letter)
192# or an UNC path with at most a / or \ after the mount point.
Guido van Rossum555915a1994-02-24 11:32:59 +0000193
194def ismount(path):
Guido van Rossumca99c2c1998-01-19 22:25:59 +0000195 """Test whether a path is a mount point (defined as root of drive)"""
Guido van Rossumf3c695c1999-04-06 19:32:19 +0000196 unc, rest = splitunc(path)
197 if unc:
198 return rest in ("", "/", "\\")
Guido van Rossumca99c2c1998-01-19 22:25:59 +0000199 p = splitdrive(path)[1]
Fred Drakeb4e460a2000-09-28 16:25:20 +0000200 return len(p) == 1 and p[0] in '/\\'
Guido van Rossum555915a1994-02-24 11:32:59 +0000201
202
203# Directory tree walk.
204# For each directory under top (including top itself, but excluding
205# '.' and '..'), func(arg, dirname, filenames) is called, where
206# dirname is the name of the directory and filenames is the list
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000207# of files (and subdirectories etc.) in the directory.
Guido van Rossum555915a1994-02-24 11:32:59 +0000208# The func may modify the filenames list, to implement a filter,
209# or to impose a different order of visiting.
210
211def walk(top, func, arg):
Tim Peterscf5e6a42001-10-10 04:16:20 +0000212 """Directory tree walk with callback function.
Guido van Rossum534972b1999-02-03 17:20:50 +0000213
Tim Peterscf5e6a42001-10-10 04:16:20 +0000214 For each directory in the directory tree rooted at top (including top
215 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
216 dirname is the name of the directory, and fnames a list of the names of
217 the files and subdirectories in dirname (excluding '.' and '..'). func
218 may modify the fnames list in-place (e.g. via del or slice assignment),
219 and walk will only recurse into the subdirectories whose names remain in
220 fnames; this can be used to implement a filter, or to impose a specific
221 order of visiting. No semantics are defined for, or required of, arg,
222 beyond that arg is always passed to func. It can be used, e.g., to pass
223 a filename pattern, or a mutable object designed to accumulate
224 statistics. Passing None for arg is common."""
Philip Jenveyd846f1d2009-05-08 02:28:39 +0000225 warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.",
226 stacklevel=2)
Guido van Rossum15e22e11997-12-05 19:03:01 +0000227 try:
228 names = os.listdir(top)
229 except os.error:
230 return
231 func(arg, top, names)
Guido van Rossum15e22e11997-12-05 19:03:01 +0000232 for name in names:
Georg Brandle2a902c2008-01-06 14:17:36 +0000233 name = join(top, name)
234 if isdir(name):
235 walk(name, func, arg)
Guido van Rossum555915a1994-02-24 11:32:59 +0000236
237
238# Expand paths beginning with '~' or '~user'.
239# '~' means $HOME; '~user' means that user's home directory.
240# If the path doesn't begin with '~', or if the user or $HOME is unknown,
241# the path is returned unchanged (leaving error reporting to whatever
242# function is called with the expanded path as argument).
243# See also module 'glob' for expansion of *, ? and [...] in pathnames.
244# (A function should also be defined to do full *sh-style environment
245# variable expansion.)
246
247def expanduser(path):
Guido van Rossum534972b1999-02-03 17:20:50 +0000248 """Expand ~ and ~user constructs.
249
250 If user or $HOME is unknown, do nothing."""
Fred Drake8152d322000-12-12 23:20:45 +0000251 if path[:1] != '~':
Guido van Rossum15e22e11997-12-05 19:03:01 +0000252 return path
253 i, n = 1, len(path)
254 while i < n and path[i] not in '/\\':
Fred Drakeb4e460a2000-09-28 16:25:20 +0000255 i = i + 1
Georg Brandl03b90d82007-03-13 22:07:36 +0000256
257 if 'HOME' in os.environ:
258 userhome = os.environ['HOME']
259 elif 'USERPROFILE' in os.environ:
260 userhome = os.environ['USERPROFILE']
261 elif not 'HOMEPATH' in os.environ:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000262 return path
Georg Brandl03b90d82007-03-13 22:07:36 +0000263 else:
264 try:
265 drive = os.environ['HOMEDRIVE']
266 except KeyError:
267 drive = ''
268 userhome = join(drive, os.environ['HOMEPATH'])
269
270 if i != 1: #~user
271 userhome = join(dirname(userhome), path[1:i])
272
Guido van Rossum15e22e11997-12-05 19:03:01 +0000273 return userhome + path[i:]
Guido van Rossum555915a1994-02-24 11:32:59 +0000274
275
276# Expand paths containing shell variable substitutions.
277# The following rules apply:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000278# - no expansion within single quotes
Georg Brandl03b90d82007-03-13 22:07:36 +0000279# - '$$' is translated into '$'
280# - '%%' is translated into '%' if '%%' are not seen in %var1%%var2%
Guido van Rossum15e22e11997-12-05 19:03:01 +0000281# - ${varname} is accepted.
Georg Brandl03b90d82007-03-13 22:07:36 +0000282# - $varname is accepted.
283# - %varname% is accepted.
284# - varnames can be made out of letters, digits and the characters '_-'
Ezio Melottic2077b02011-03-16 12:34:31 +0200285# (though is not verified in the ${varname} and %varname% cases)
Guido van Rossum555915a1994-02-24 11:32:59 +0000286# XXX With COMMAND.COM you can use any characters in a variable name,
287# XXX except '^|<>='.
288
Tim Peters2344fae2001-01-15 00:50:52 +0000289def expandvars(path):
Georg Brandl03b90d82007-03-13 22:07:36 +0000290 """Expand shell variables of the forms $var, ${var} and %var%.
Guido van Rossum534972b1999-02-03 17:20:50 +0000291
292 Unknown variables are left unchanged."""
Georg Brandl03b90d82007-03-13 22:07:36 +0000293 if '$' not in path and '%' not in path:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000294 return path
Fred Drakeb4e460a2000-09-28 16:25:20 +0000295 import string
Fred Drake79e75e12001-07-20 19:05:50 +0000296 varchars = string.ascii_letters + string.digits + '_-'
Guido van Rossum15e22e11997-12-05 19:03:01 +0000297 res = ''
298 index = 0
299 pathlen = len(path)
300 while index < pathlen:
301 c = path[index]
302 if c == '\'': # no expansion within single quotes
303 path = path[index + 1:]
304 pathlen = len(path)
305 try:
Fred Drakeb4e460a2000-09-28 16:25:20 +0000306 index = path.index('\'')
Guido van Rossum15e22e11997-12-05 19:03:01 +0000307 res = res + '\'' + path[:index + 1]
Fred Drakeb4e460a2000-09-28 16:25:20 +0000308 except ValueError:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000309 res = res + path
Fred Drakeb4e460a2000-09-28 16:25:20 +0000310 index = pathlen - 1
Georg Brandl03b90d82007-03-13 22:07:36 +0000311 elif c == '%': # variable or '%'
312 if path[index + 1:index + 2] == '%':
313 res = res + c
314 index = index + 1
315 else:
316 path = path[index+1:]
317 pathlen = len(path)
318 try:
319 index = path.index('%')
320 except ValueError:
321 res = res + '%' + path
322 index = pathlen - 1
323 else:
324 var = path[:index]
325 if var in os.environ:
326 res = res + os.environ[var]
327 else:
328 res = res + '%' + var + '%'
Guido van Rossum15e22e11997-12-05 19:03:01 +0000329 elif c == '$': # variable or '$$'
330 if path[index + 1:index + 2] == '$':
331 res = res + c
332 index = index + 1
333 elif path[index + 1:index + 2] == '{':
334 path = path[index+2:]
335 pathlen = len(path)
336 try:
Fred Drakeb4e460a2000-09-28 16:25:20 +0000337 index = path.index('}')
Guido van Rossum15e22e11997-12-05 19:03:01 +0000338 var = path[:index]
Raymond Hettinger54f02222002-06-01 14:18:47 +0000339 if var in os.environ:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000340 res = res + os.environ[var]
Sjoerd Mullender33a0a062007-01-16 16:42:38 +0000341 else:
342 res = res + '${' + var + '}'
Fred Drakeb4e460a2000-09-28 16:25:20 +0000343 except ValueError:
Sjoerd Mullender33a0a062007-01-16 16:42:38 +0000344 res = res + '${' + path
Guido van Rossum15e22e11997-12-05 19:03:01 +0000345 index = pathlen - 1
346 else:
347 var = ''
348 index = index + 1
349 c = path[index:index + 1]
350 while c != '' and c in varchars:
351 var = var + c
352 index = index + 1
353 c = path[index:index + 1]
Raymond Hettinger54f02222002-06-01 14:18:47 +0000354 if var in os.environ:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000355 res = res + os.environ[var]
Sjoerd Mullender33a0a062007-01-16 16:42:38 +0000356 else:
357 res = res + '$' + var
Guido van Rossum15e22e11997-12-05 19:03:01 +0000358 if c != '':
Sjoerd Mullender33a0a062007-01-16 16:42:38 +0000359 index = index - 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000360 else:
361 res = res + c
362 index = index + 1
363 return res
Guido van Rossum555915a1994-02-24 11:32:59 +0000364
365
Tim Peters54a14a32001-08-30 22:05:26 +0000366# 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 +0000367# Previously, this function also truncated pathnames to 8+3 format,
368# but as this module is called "ntpath", that's obviously wrong!
Guido van Rossum555915a1994-02-24 11:32:59 +0000369
370def normpath(path):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000371 """Normalize path, eliminating double slashes, etc."""
Ezio Melottib5689de2010-01-12 03:32:05 +0000372 # Preserve unicode (if path is unicode)
373 backslash, dot = (u'\\', u'.') if isinstance(path, unicode) else ('\\', '.')
Georg Brandle2773252010-08-01 19:14:56 +0000374 if path.startswith(('\\\\.\\', '\\\\?\\')):
375 # in the case of paths with these prefixes:
376 # \\.\ -> device names
377 # \\?\ -> literal paths
378 # do not do any normalization, but return the path unchanged
379 return path
Fred Drakeb4e460a2000-09-28 16:25:20 +0000380 path = path.replace("/", "\\")
Guido van Rossum15e22e11997-12-05 19:03:01 +0000381 prefix, path = splitdrive(path)
Brett Cannonbdc36272004-07-10 20:42:22 +0000382 # We need to be careful here. If the prefix is empty, and the path starts
383 # with a backslash, it could either be an absolute path on the current
384 # drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It
385 # is therefore imperative NOT to collapse multiple backslashes blindly in
386 # that case.
387 # The code below preserves multiple backslashes when there is no drive
388 # letter. This means that the invalid filename \\\a\b is preserved
389 # unchanged, where a\\\b is normalised to a\b. It's not clear that there
390 # is any better behaviour for such edge cases.
391 if prefix == '':
392 # No drive letter - preserve initial backslashes
393 while path[:1] == "\\":
Ezio Melottib5689de2010-01-12 03:32:05 +0000394 prefix = prefix + backslash
Brett Cannonbdc36272004-07-10 20:42:22 +0000395 path = path[1:]
396 else:
397 # We have a drive letter - collapse initial backslashes
398 if path.startswith("\\"):
Ezio Melottib5689de2010-01-12 03:32:05 +0000399 prefix = prefix + backslash
Brett Cannonbdc36272004-07-10 20:42:22 +0000400 path = path.lstrip("\\")
Fred Drakeb4e460a2000-09-28 16:25:20 +0000401 comps = path.split("\\")
Guido van Rossum15e22e11997-12-05 19:03:01 +0000402 i = 0
403 while i < len(comps):
Tim Peters54a14a32001-08-30 22:05:26 +0000404 if comps[i] in ('.', ''):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000405 del comps[i]
Tim Peters54a14a32001-08-30 22:05:26 +0000406 elif comps[i] == '..':
407 if i > 0 and comps[i-1] != '..':
408 del comps[i-1:i+1]
409 i -= 1
410 elif i == 0 and prefix.endswith("\\"):
411 del comps[i]
412 else:
413 i += 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000414 else:
Tim Peters54a14a32001-08-30 22:05:26 +0000415 i += 1
Guido van Rossum15e22e11997-12-05 19:03:01 +0000416 # If the path is now empty, substitute '.'
417 if not prefix and not comps:
Ezio Melottib5689de2010-01-12 03:32:05 +0000418 comps.append(dot)
419 return prefix + backslash.join(comps)
Guido van Rossume294cf61999-01-29 18:05:18 +0000420
421
422# Return an absolute path.
Tim Peters21fbd572006-04-21 21:18:10 +0000423try:
424 from nt import _getfullpathname
Mark Hammondf717f052002-01-17 00:44:26 +0000425
Tim Peters21fbd572006-04-21 21:18:10 +0000426except ImportError: # not running on Windows - mock up something sensible
427 def abspath(path):
428 """Return the absolute version of a path."""
429 if not isabs(path):
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000430 if isinstance(path, unicode):
431 cwd = os.getcwdu()
432 else:
433 cwd = os.getcwd()
434 path = join(cwd, path)
Tim Peters21fbd572006-04-21 21:18:10 +0000435 return normpath(path)
436
437else: # use native Windows method on Windows
438 def abspath(path):
439 """Return the absolute version of a path."""
440
441 if path: # Empty path must return current working directory.
442 try:
443 path = _getfullpathname(path)
444 except WindowsError:
445 pass # Bad path - return unchanged.
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000446 elif isinstance(path, unicode):
447 path = os.getcwdu()
Tim Peters21fbd572006-04-21 21:18:10 +0000448 else:
449 path = os.getcwd()
450 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000451
452# realpath is a no-op on systems without islink support
453realpath = abspath
Mark Hammond8696ebc2002-10-08 02:44:31 +0000454# Win9x family and earlier have no Unicode filename support.
Tim Peters26bc25a2002-10-09 07:56:04 +0000455supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
456 sys.getwindowsversion()[3] >= 2)
Collin Winter6f187742007-03-16 22:16:08 +0000457
Hirokazu Yamamoto8596ef72010-10-19 01:23:51 +0000458def _abspath_split(path):
459 abs = abspath(normpath(path))
460 prefix, rest = splitunc(abs)
461 is_unc = bool(prefix)
462 if not is_unc:
463 prefix, rest = splitdrive(abs)
464 return is_unc, prefix, [x for x in rest.split(sep) if x]
465
Collin Winter6f187742007-03-16 22:16:08 +0000466def relpath(path, start=curdir):
467 """Return a relative version of a path"""
468
469 if not path:
470 raise ValueError("no path specified")
Hirokazu Yamamoto8596ef72010-10-19 01:23:51 +0000471
472 start_is_unc, start_prefix, start_list = _abspath_split(start)
473 path_is_unc, path_prefix, path_list = _abspath_split(path)
474
475 if path_is_unc ^ start_is_unc:
476 raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
477 % (path, start))
478 if path_prefix.lower() != start_prefix.lower():
479 if path_is_unc:
480 raise ValueError("path is on UNC root %s, start on UNC root %s"
481 % (path_prefix, start_prefix))
Collin Winter6f187742007-03-16 22:16:08 +0000482 else:
483 raise ValueError("path is on drive %s, start on drive %s"
Hirokazu Yamamoto8596ef72010-10-19 01:23:51 +0000484 % (path_prefix, start_prefix))
Collin Winter6f187742007-03-16 22:16:08 +0000485 # Work out how much of the filepath is shared by start and path.
Hirokazu Yamamoto8596ef72010-10-19 01:23:51 +0000486 i = 0
487 for e1, e2 in zip(start_list, path_list):
488 if e1.lower() != e2.lower():
Collin Winter6f187742007-03-16 22:16:08 +0000489 break
Collin Winter6f187742007-03-16 22:16:08 +0000490 i += 1
491
492 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
Georg Brandl183a0842008-01-06 14:27:15 +0000493 if not rel_list:
494 return curdir
Collin Winter6f187742007-03-16 22:16:08 +0000495 return join(*rel_list)
Brian Curtincaea7e82011-06-08 19:29:53 -0500496
497try:
498 # The genericpath.isdir implementation uses os.stat and checks the mode
499 # attribute to tell whether or not the path is a directory.
500 # This is overkill on Windows - just pass the path to GetFileAttributes
501 # and check the attribute from there.
Brian Curtin5446f082011-06-09 10:00:42 -0500502 from nt import _isdir as isdir
Brian Curtincaea7e82011-06-08 19:29:53 -0500503except ImportError:
Brian Curtin5446f082011-06-09 10:00:42 -0500504 # Use genericpath.isdir as imported above.
505 pass