blob: f8334e55183ca1ab7ad74e27a2ce183bb77812f3 [file] [log] [blame]
Guido van Rossum15e22e11997-12-05 19:03:01 +00001# Module 'ntpath' -- common operations on WinNT/Win95 pathnames
2"""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
9import stat
10import string
Skip Montanaro1d3dd742000-07-17 03:06:58 +000011import re
Skip Montanaro4d5d5bf2000-07-13 01:01:03 +000012
Guido van Rossum555915a1994-02-24 11:32:59 +000013
Guido van Rossume2ad88c1997-08-12 14:46:58 +000014# Normalize the case of a pathname and map slashes to backslashes.
15# Other normalizations (such as optimizing '../' away) are not done
Guido van Rossum555915a1994-02-24 11:32:59 +000016# (this is done by normpath).
Guido van Rossume2ad88c1997-08-12 14:46:58 +000017
Guido van Rossum555915a1994-02-24 11:32:59 +000018def normcase(s):
Guido van Rossum16a0bc21998-02-18 13:48:31 +000019 """Normalize case of pathname.
20
Guido van Rossum534972b1999-02-03 17:20:50 +000021 Makes all characters lowercase and all slashes into backslashes."""
Guido van Rossum16a0bc21998-02-18 13:48:31 +000022 return string.lower(string.replace(s, "/", "\\"))
Guido van Rossum555915a1994-02-24 11:32:59 +000023
Guido van Rossum77e1db31997-06-02 23:11:57 +000024
Fred Drakeef0b5dd2000-02-17 17:30:40 +000025# Return whether a path is absolute.
Guido van Rossum555915a1994-02-24 11:32:59 +000026# Trivial in Posix, harder on the Mac or MS-DOS.
27# For DOS it is absolute if it starts with a slash or backslash (current
Guido van Rossum534972b1999-02-03 17:20:50 +000028# volume), or if a pathname after the volume letter and colon / UNC resource
29# starts with a slash or backslash.
Guido van Rossum555915a1994-02-24 11:32:59 +000030
31def isabs(s):
Guido van Rossum15e22e11997-12-05 19:03:01 +000032 """Test whether a path is absolute"""
33 s = splitdrive(s)[1]
34 return s != '' and s[:1] in '/\\'
Guido van Rossum555915a1994-02-24 11:32:59 +000035
36
Guido van Rossum77e1db31997-06-02 23:11:57 +000037# Join two (or more) paths.
38
Barry Warsaw384d2491997-02-18 21:53:25 +000039def join(a, *p):
Guido van Rossum15e22e11997-12-05 19:03:01 +000040 """Join two or more pathname components, inserting "\\" as needed"""
41 path = a
42 for b in p:
43 if isabs(b):
44 path = b
45 elif path == '' or path[-1:] in '/\\':
46 path = path + b
47 else:
48 path = path + os.sep + b
49 return path
Guido van Rossum555915a1994-02-24 11:32:59 +000050
51
52# Split a path in a drive specification (a drive letter followed by a
Guido van Rossumf3c695c1999-04-06 19:32:19 +000053# colon) and the path specification.
Guido van Rossum555915a1994-02-24 11:32:59 +000054# It is always true that drivespec + pathspec == p
55def splitdrive(p):
Guido van Rossumf3c695c1999-04-06 19:32:19 +000056 """Split a pathname into drive and path specifiers. Returns a 2-tuple
57"(drive,path)"; either part may be empty"""
Guido van Rossum15e22e11997-12-05 19:03:01 +000058 if p[1:2] == ':':
59 return p[0:2], p[2:]
Guido van Rossumf3c695c1999-04-06 19:32:19 +000060 return '', p
61
62
63# Parse UNC paths
64def splitunc(p):
65 """Split a pathname into UNC mount point and relative path specifiers.
66
67 Return a 2-tuple (unc, rest); either part may be empty.
68 If unc is not empty, it has the form '//host/mount' (or similar
69 using backslashes). unc+rest is always the input path.
70 Paths containing drive letters never have an UNC part.
71 """
72 if p[1:2] == ':':
73 return '', p # Drive letter present
Guido van Rossum534972b1999-02-03 17:20:50 +000074 firstTwo = p[0:2]
75 if firstTwo == '//' or firstTwo == '\\\\':
76 # is a UNC path:
77 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
78 # \\machine\mountpoint\directories...
79 # directory ^^^^^^^^^^^^^^^
80 normp = normcase(p)
81 index = string.find(normp, '\\', 2)
82 if index == -1:
83 ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
84 return ("", p)
85 index = string.find(normp, '\\', index + 1)
86 if index == -1:
87 index = len(p)
88 return p[:index], p[index:]
Guido van Rossum15e22e11997-12-05 19:03:01 +000089 return '', p
Guido van Rossum555915a1994-02-24 11:32:59 +000090
91
92# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +000093# rest). After the trailing '/' is stripped, the invariant
Guido van Rossum555915a1994-02-24 11:32:59 +000094# join(head, tail) == p holds.
95# The resulting head won't end in '/' unless it is the root.
96
97def split(p):
Guido van Rossum534972b1999-02-03 17:20:50 +000098 """Split a pathname.
99
100 Return tuple (head, tail) where tail is everything after the final slash.
101 Either part may be empty."""
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000102
Guido van Rossum15e22e11997-12-05 19:03:01 +0000103 d, p = splitdrive(p)
Guido van Rossum8f0fa9e1999-03-19 21:05:12 +0000104 # set i to index beyond p's last slash
105 i = len(p)
106 while i and p[i-1] not in '/\\':
107 i = i - 1
108 head, tail = p[:i], p[i:] # now tail has no slashes
109 # remove trailing slashes from head, unless it's all slashes
110 head2 = head
111 while head2 and head2[-1] in '/\\':
112 head2 = head2[:-1]
113 head = head2 or head
Guido van Rossum15e22e11997-12-05 19:03:01 +0000114 return d + head, tail
Guido van Rossum555915a1994-02-24 11:32:59 +0000115
116
117# Split a path in root and extension.
Guido van Rossum73e122f1997-01-22 00:17:26 +0000118# The extension is everything starting at the last dot in the last
Guido van Rossum555915a1994-02-24 11:32:59 +0000119# pathname component; the root is everything before that.
120# It is always true that root + ext == p.
121
122def splitext(p):
Guido van Rossum534972b1999-02-03 17:20:50 +0000123 """Split the extension from a pathname.
124
125 Extension is everything from the last dot to the end.
126 Return (root, ext), either part may be empty."""
Guido van Rossum15e22e11997-12-05 19:03:01 +0000127 root, ext = '', ''
128 for c in p:
129 if c in ['/','\\']:
130 root, ext = root + ext + c, ''
131 elif c == '.':
132 if ext:
133 root, ext = root + ext, c
134 else:
135 ext = c
136 elif ext:
137 ext = ext + c
138 else:
139 root = root + c
140 return root, ext
Guido van Rossum555915a1994-02-24 11:32:59 +0000141
142
143# Return the tail (basename) part of a path.
144
145def basename(p):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000146 """Returns the final component of a pathname"""
147 return split(p)[1]
Guido van Rossum555915a1994-02-24 11:32:59 +0000148
149
150# Return the head (dirname) part of a path.
151
152def dirname(p):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000153 """Returns the directory component of a pathname"""
154 return split(p)[0]
Guido van Rossum555915a1994-02-24 11:32:59 +0000155
156
157# Return the longest prefix of all list elements.
158
159def commonprefix(m):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000160 "Given a list of pathnames, returns the longest common leading component"
161 if not m: return ''
Skip Montanaro1d3dd742000-07-17 03:06:58 +0000162 n = map(string.lower, m)
Skip Montanaro97bc98a2000-07-12 16:55:57 +0000163 for i in range(len(n)):
Skip Montanaro1d3dd742000-07-17 03:06:58 +0000164 n[i] = re.split(r"[/\\]", n[i])
165
Skip Montanaro97bc98a2000-07-12 16:55:57 +0000166 prefix = n[0]
167 for item in n:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000168 for i in range(len(prefix)):
169 if prefix[:i+1] <> item[:i+1]:
170 prefix = prefix[:i]
171 if i == 0: return ''
172 break
Skip Montanaro1d3dd742000-07-17 03:06:58 +0000173 return "\\".join(prefix)
Guido van Rossum555915a1994-02-24 11:32:59 +0000174
175
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000176# Get size, mtime, atime of files.
177
178def getsize(filename):
Guido van Rossum534972b1999-02-03 17:20:50 +0000179 """Return the size of a file, reported by os.stat()"""
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000180 st = os.stat(filename)
181 return st[stat.ST_SIZE]
182
183def getmtime(filename):
Guido van Rossum534972b1999-02-03 17:20:50 +0000184 """Return the last modification time of a file, reported by os.stat()"""
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000185 st = os.stat(filename)
186 return st[stat.ST_MTIME]
187
188def getatime(filename):
Guido van Rossum534972b1999-02-03 17:20:50 +0000189 """Return the last access time of a file, reported by os.stat()"""
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000190 st = os.stat(filename)
Fred Drake162bd852000-07-01 06:36:51 +0000191 return st[stat.ST_ATIME]
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000192
193
Guido van Rossum555915a1994-02-24 11:32:59 +0000194# Is a path a symbolic link?
195# This will always return false on systems where posix.lstat doesn't exist.
196
197def islink(path):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000198 """Test for symbolic link. On WindowsNT/95 always returns false"""
199 return 0
Guido van Rossum555915a1994-02-24 11:32:59 +0000200
201
202# Does a path exist?
203# This is false for dangling symbolic links.
204
205def exists(path):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000206 """Test whether a path exists"""
207 try:
208 st = os.stat(path)
209 except os.error:
210 return 0
211 return 1
Guido van Rossum555915a1994-02-24 11:32:59 +0000212
213
214# Is a path a dos directory?
215# This follows symbolic links, so both islink() and isdir() can be true
216# for the same path.
217
218def isdir(path):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000219 """Test whether a path is a directory"""
220 try:
221 st = os.stat(path)
222 except os.error:
223 return 0
224 return stat.S_ISDIR(st[stat.ST_MODE])
Guido van Rossum555915a1994-02-24 11:32:59 +0000225
226
227# Is a path a regular file?
228# This follows symbolic links, so both islink() and isdir() can be true
229# for the same path.
230
231def isfile(path):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000232 """Test whether a path is a regular file"""
233 try:
234 st = os.stat(path)
235 except os.error:
236 return 0
237 return stat.S_ISREG(st[stat.ST_MODE])
Guido van Rossum555915a1994-02-24 11:32:59 +0000238
239
Guido van Rossumf3c695c1999-04-06 19:32:19 +0000240# Is a path a mount point? Either a root (with or without drive letter)
241# or an UNC path with at most a / or \ after the mount point.
Guido van Rossum555915a1994-02-24 11:32:59 +0000242
243def ismount(path):
Guido van Rossumca99c2c1998-01-19 22:25:59 +0000244 """Test whether a path is a mount point (defined as root of drive)"""
Guido van Rossumf3c695c1999-04-06 19:32:19 +0000245 unc, rest = splitunc(path)
246 if unc:
247 return rest in ("", "/", "\\")
Guido van Rossumca99c2c1998-01-19 22:25:59 +0000248 p = splitdrive(path)[1]
249 return len(p)==1 and p[0] in '/\\'
Guido van Rossum555915a1994-02-24 11:32:59 +0000250
251
252# Directory tree walk.
253# For each directory under top (including top itself, but excluding
254# '.' and '..'), func(arg, dirname, filenames) is called, where
255# dirname is the name of the directory and filenames is the list
256# files files (and subdirectories etc.) in the directory.
257# The func may modify the filenames list, to implement a filter,
258# or to impose a different order of visiting.
259
260def walk(top, func, arg):
Guido van Rossum534972b1999-02-03 17:20:50 +0000261 """Directory tree walk whth callback function.
262
Guido van Rossumf618a481999-11-02 13:29:08 +0000263 walk(top, func, arg) calls func(arg, d, files) for each directory d
Guido van Rossum534972b1999-02-03 17:20:50 +0000264 in the tree rooted at top (including top itself); files is a list
265 of all the files and subdirs in directory d."""
Guido van Rossum15e22e11997-12-05 19:03:01 +0000266 try:
267 names = os.listdir(top)
268 except os.error:
269 return
270 func(arg, top, names)
271 exceptions = ('.', '..')
272 for name in names:
273 if name not in exceptions:
274 name = join(top, name)
275 if isdir(name):
276 walk(name, func, arg)
Guido van Rossum555915a1994-02-24 11:32:59 +0000277
278
279# Expand paths beginning with '~' or '~user'.
280# '~' means $HOME; '~user' means that user's home directory.
281# If the path doesn't begin with '~', or if the user or $HOME is unknown,
282# the path is returned unchanged (leaving error reporting to whatever
283# function is called with the expanded path as argument).
284# See also module 'glob' for expansion of *, ? and [...] in pathnames.
285# (A function should also be defined to do full *sh-style environment
286# variable expansion.)
287
288def expanduser(path):
Guido van Rossum534972b1999-02-03 17:20:50 +0000289 """Expand ~ and ~user constructs.
290
291 If user or $HOME is unknown, do nothing."""
Guido van Rossum15e22e11997-12-05 19:03:01 +0000292 if path[:1] <> '~':
293 return path
294 i, n = 1, len(path)
295 while i < n and path[i] not in '/\\':
296 i = i+1
297 if i == 1:
298 if os.environ.has_key('HOME'):
299 userhome = os.environ['HOME']
300 elif not os.environ.has_key('HOMEPATH'):
301 return path
302 else:
303 try:
304 drive=os.environ['HOMEDRIVE']
305 except KeyError:
306 drive = ''
307 userhome = join(drive, os.environ['HOMEPATH'])
308 else:
309 return path
310 return userhome + path[i:]
Guido van Rossum555915a1994-02-24 11:32:59 +0000311
312
313# Expand paths containing shell variable substitutions.
314# The following rules apply:
Guido van Rossum15e22e11997-12-05 19:03:01 +0000315# - no expansion within single quotes
316# - no escape character, except for '$$' which is translated into '$'
317# - ${varname} is accepted.
318# - varnames can be made out of letters, digits and the character '_'
Guido van Rossum555915a1994-02-24 11:32:59 +0000319# XXX With COMMAND.COM you can use any characters in a variable name,
320# XXX except '^|<>='.
321
322varchars = string.letters + string.digits + '_-'
323
Guido van Rossum15e22e11997-12-05 19:03:01 +0000324def expandvars(path):
Guido van Rossum534972b1999-02-03 17:20:50 +0000325 """Expand shell variables of form $var and ${var}.
326
327 Unknown variables are left unchanged."""
Guido van Rossum15e22e11997-12-05 19:03:01 +0000328 if '$' not in path:
329 return path
330 res = ''
331 index = 0
332 pathlen = len(path)
333 while index < pathlen:
334 c = path[index]
335 if c == '\'': # no expansion within single quotes
336 path = path[index + 1:]
337 pathlen = len(path)
338 try:
339 index = string.index(path, '\'')
340 res = res + '\'' + path[:index + 1]
341 except string.index_error:
342 res = res + path
343 index = pathlen -1
344 elif c == '$': # variable or '$$'
345 if path[index + 1:index + 2] == '$':
346 res = res + c
347 index = index + 1
348 elif path[index + 1:index + 2] == '{':
349 path = path[index+2:]
350 pathlen = len(path)
351 try:
352 index = string.index(path, '}')
353 var = path[:index]
354 if os.environ.has_key(var):
355 res = res + os.environ[var]
356 except string.index_error:
357 res = res + path
358 index = pathlen - 1
359 else:
360 var = ''
361 index = index + 1
362 c = path[index:index + 1]
363 while c != '' and c in varchars:
364 var = var + c
365 index = index + 1
366 c = path[index:index + 1]
367 if os.environ.has_key(var):
368 res = res + os.environ[var]
369 if c != '':
370 res = res + c
371 else:
372 res = res + c
373 index = index + 1
374 return res
Guido van Rossum555915a1994-02-24 11:32:59 +0000375
376
377# 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 +0000378# Previously, this function also truncated pathnames to 8+3 format,
379# but as this module is called "ntpath", that's obviously wrong!
Guido van Rossum555915a1994-02-24 11:32:59 +0000380
381def normpath(path):
Guido van Rossum15e22e11997-12-05 19:03:01 +0000382 """Normalize path, eliminating double slashes, etc."""
Guido van Rossum16a0bc21998-02-18 13:48:31 +0000383 path = string.replace(path, "/", "\\")
Guido van Rossum15e22e11997-12-05 19:03:01 +0000384 prefix, path = splitdrive(path)
385 while path[:1] == os.sep:
386 prefix = prefix + os.sep
387 path = path[1:]
388 comps = string.splitfields(path, os.sep)
389 i = 0
390 while i < len(comps):
391 if comps[i] == '.':
392 del comps[i]
393 elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
394 del comps[i-1:i+1]
395 i = i-1
396 elif comps[i] == '' and i > 0 and comps[i-1] <> '':
397 del comps[i]
398 else:
399 i = i+1
400 # If the path is now empty, substitute '.'
401 if not prefix and not comps:
402 comps.append('.')
403 return prefix + string.joinfields(comps, os.sep)
Guido van Rossume294cf61999-01-29 18:05:18 +0000404
405
406# Return an absolute path.
407def abspath(path):
Guido van Rossum534972b1999-02-03 17:20:50 +0000408 """Return the absolute version of a path"""
Guido van Rossum9787bea1999-01-29 22:30:41 +0000409 try:
410 import win32api
Guido van Rossum9787bea1999-01-29 22:30:41 +0000411 except ImportError:
Guido van Rossum823e91c2000-02-02 16:54:39 +0000412 global abspath
413 def _abspath(path):
414 if not isabs(path):
415 path = join(os.getcwd(), path)
416 return normpath(path)
417 abspath = _abspath
418 return _abspath(path)
Mark Hammond647d2fe2000-08-14 06:20:32 +0000419 if path: # Empty path must return current working directory.
420 try:
421 path = win32api.GetFullPathName(path)
422 except win32api.error:
423 pass # Bad path - return unchanged.
424 else:
425 path = os.getcwd()
Guido van Rossum6dfc7921999-11-30 15:00:00 +0000426 return normpath(path)