2 """Common pathname manipulations, WindowsNT/95 version.
4 Instead of importing this module directly, import os and refer to this
11 __all__ = [
"normcase",
"isabs",
"join",
"splitdrive",
"split",
"splitext",
12 "basename",
"dirname",
"commonprefix",
"getsize",
"getmtime",
13 "getatime",
"islink",
"exists",
"isdir",
"isfile",
"ismount",
14 "walk",
"expanduser",
"expandvars",
"normpath",
"abspath",
"splitunc"]
21 """Normalize case of pathname.
23 Makes all characters lowercase and all slashes into backslashes."""
24 return s.replace(
"/",
"\\").
lower()
34 """Test whether a path is absolute"""
36 return s !=
'' and s[:1]
in '/\\'
42 """Join two or more pathname components, inserting "\\" as needed"""
58 if path[1:2] !=
":" or b[1:2] ==
":":
63 elif len(path) > 3
or (len(path) == 3
and
64 path[-1]
not in "/\\"):
74 if b
and b[0]
in "/\\":
99 """Split a pathname into drive and path specifiers. Returns a 2-tuple
100 "(drive,path)"; either part may be empty"""
108 """Split a pathname into UNC mount point and relative path specifiers.
110 Return a 2-tuple (unc, rest); either part may be empty.
111 If unc is not empty, it has the form '//host/mount' (or similar
112 using backslashes). unc+rest is always the input path.
113 Paths containing drive letters never have an UNC part.
118 if firstTwo ==
'//' or firstTwo ==
'\\\\':
124 index = normp.find(
'\\', 2)
128 index = normp.find(
'\\', index + 1)
131 return p[:index], p[index:]
143 Return tuple (head, tail) where tail is everything after the final slash.
144 Either part may be empty."""
149 while i
and p[i-1]
not in '/\\':
151 head, tail = p[:i], p[i:]
154 while head2
and head2[-1]
in '/\\':
157 return d + head, tail
166 """Split the extension from a pathname.
168 Extension is everything from the last dot to the end.
169 Return (root, ext), either part may be empty."""
173 root, ext = root + ext + c,
''
176 root, ext = root + ext, c
189 """Returns the final component of a pathname"""
196 """Returns the directory component of a pathname"""
203 "Given a list of pathnames, returns the longest common leading component"
207 for i
in range(len(prefix)):
208 if prefix[:i+1] != item[:i+1]:
218 """Return the size of a file, reported by os.stat()"""
219 st = os.stat(filename)
220 return st[stat.ST_SIZE]
223 """Return the last modification time of a file, reported by os.stat()"""
224 st = os.stat(filename)
225 return st[stat.ST_MTIME]
228 """Return the last access time of a file, reported by os.stat()"""
229 st = os.stat(filename)
230 return st[stat.ST_ATIME]
237 """Test for symbolic link. On WindowsNT/95 always returns false"""
245 """Test whether a path exists"""
258 """Test whether a path is a directory"""
271 """Test whether a path is a regular file"""
283 """Test whether a path is a mount point (defined as root of drive)"""
286 return rest
in (
"",
"/",
"\\")
288 return len(p) == 1
and p[0]
in '/\\'
300 """Directory tree walk with callback function.
302 For each directory in the directory tree rooted at top (including top
303 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
304 dirname is the name of the directory, and fnames a list of the names of
305 the files and subdirectories in dirname (excluding '.' and '..'). func
306 may modify the fnames list in-place (e.g. via del or slice assignment),
307 and walk will only recurse into the subdirectories whose names remain in
308 fnames; this can be used to implement a filter, or to impose a specific
309 order of visiting. No semantics are defined for, or required of, arg,
310 beyond that arg is always passed to func. It can be used, e.g., to pass
311 a filename pattern, or a mutable object designed to accumulate
312 statistics. Passing None for arg is common."""
315 names = os.listdir(top)
318 func(arg, top, names)
319 exceptions = (
'.',
'..')
321 if name
not in exceptions:
322 name =
join(top, name)
324 walk(name, func, arg)
337 """Expand ~ and ~user constructs.
339 If user or $HOME is unknown, do nothing."""
343 while i < n
and path[i]
not in '/\\':
346 if os.environ.has_key(
'HOME'):
347 userhome = os.environ[
'HOME']
348 elif not os.environ.has_key(
'HOMEPATH'):
352 drive = os.environ[
'HOMEDRIVE']
355 userhome =
join(drive, os.environ[
'HOMEPATH'])
358 return userhome + path[i:]
371 """Expand shell variables of form $var and ${var}.
373 Unknown variables are left unchanged."""
377 varchars = string.ascii_letters + string.digits +
'_-'
381 while index < pathlen:
384 path = path[index + 1:]
387 index = path.index(
'\'')
388 res = res +
'\'' + path[:index + 1]
393 if path[index + 1:index + 2] ==
'$':
396 elif path[index + 1:index + 2] ==
'{':
397 path = path[index+2:]
400 index = path.index(
'}')
402 if os.environ.has_key(var):
403 res = res + os.environ[var]
410 c = path[index:index + 1]
411 while c !=
'' and c
in varchars:
414 c = path[index:index + 1]
415 if os.environ.has_key(var):
416 res = res + os.environ[var]
430 """Normalize path, eliminating double slashes, etc."""
431 path = path.replace(
"/",
"\\")
433 while path[:1] ==
"\\":
434 prefix = prefix +
"\\"
436 comps = path.split(
"\\")
438 while i < len(comps):
439 if comps[i]
in (
'.',
''):
441 elif comps[i] ==
'..':
442 if i > 0
and comps[i-1] !=
'..':
445 elif i == 0
and prefix.endswith(
"\\"):
452 if not prefix
and not comps:
454 return prefix +
"\\".
join(comps)
459 """Return the absolute version of a path"""
461 from nt
import _getfullpathname
463 path = _getfullpathname(path)