1 """Common operations on DOS pathnames."""
6 __all__ = [
"normcase",
"isabs",
"join",
"splitdrive",
"split",
"splitext",
7 "basename",
"dirname",
"commonprefix",
"getsize",
"getmtime",
8 "getatime",
"islink",
"exists",
"isdir",
"isfile",
"ismount",
9 "walk",
"expanduser",
"expandvars",
"normpath",
"abspath"]
12 """Normalize the case of a pathname.
13 On MS-DOS it maps the pathname to lowercase, turns slashes into
15 Other normalizations (such as optimizing '../' away) are not allowed
16 (this is done by normpath).
17 Previously, this version mapped invalid consecutive characters to a
18 single '_', but this has been removed. This functionality should
19 possibly be added as a new function."""
21 return s.replace(
"/",
"\\").
lower()
25 """Return whether a path is absolute.
26 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
28 volume), or if a pathname after the volume letter and colon starts with
29 a slash or backslash."""
32 return s !=
'' and s[:1]
in '/\\'
36 """Join two (or more) paths."""
42 elif path ==
'' or path[-1:]
in '/\\:':
45 path = path +
"\\" + b
50 """Split a path into a drive specification (a drive letter followed
51 by a colon) and path specification.
52 It is always true that drivespec + pathspec == p."""
60 """Split a path into head (everything up to the last '/') and tail
61 (the rest). After the trailing '/' is stripped, the invariant
62 join(head, tail) == p holds.
63 The resulting head won't end in '/' unless it is the root."""
68 while i
and p[i-1]
not in '/\\':
70 head, tail = p[:i], p[i:]
73 while head2
and head2[-1]
in '/\\':
80 """Split a path into root and extension.
81 The extension is everything starting at the first dot in the last
82 pathname component; the root is everything before that.
83 It is always true that root + ext == p."""
88 root, ext = root + ext + c,
''
97 """Return the tail (basename) part of a path."""
103 """Return the head (dirname) part of a path."""
109 """Return the longest prefix of all list elements."""
114 for i
in range(len(prefix)):
115 if prefix[:i+1] != item[:i+1]:
125 """Return the size of a file, reported by os.stat()."""
126 st = os.stat(filename)
127 return st[stat.ST_SIZE]
130 """Return the last modification time of a file, reported by os.stat()."""
131 st = os.stat(filename)
132 return st[stat.ST_MTIME]
135 """Return the last access time of a file, reported by os.stat()."""
136 st = os.stat(filename)
137 return st[stat.ST_ATIME]
141 """Is a path a symbolic link?
142 This will always return false on systems where posix.lstat doesn't exist."""
148 """Does a path exist?
149 This is false for dangling symbolic links."""
159 """Is a path a dos directory?"""
169 """Is a path a regular file?"""
179 """Is a path a mount point?"""
186 """Directory tree walk with callback function.
188 For each directory in the directory tree rooted at top (including top
189 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
190 dirname is the name of the directory, and fnames a list of the names of
191 the files and subdirectories in dirname (excluding '.' and '..'). func
192 may modify the fnames list in-place (e.g. via del or slice assignment),
193 and walk will only recurse into the subdirectories whose names remain in
194 fnames; this can be used to implement a filter, or to impose a specific
195 order of visiting. No semantics are defined for, or required of, arg,
196 beyond that arg is always passed to func. It can be used, e.g., to pass
197 a filename pattern, or a mutable object designed to accumulate
198 statistics. Passing None for arg is common."""
201 names = os.listdir(top)
204 func(arg, top, names)
205 exceptions = (
'.',
'..')
207 if name
not in exceptions:
208 name =
join(top, name)
210 walk(name, func, arg)
214 """Expand paths beginning with '~' or '~user'.
215 '~' means $HOME; '~user' means that user's home directory.
216 If the path doesn't begin with '~', or if the user or $HOME is unknown,
217 the path is returned unchanged (leaving error reporting to whatever
218 function is called with the expanded path as argument).
219 See also module 'glob' for expansion of *, ? and [...] in pathnames.
220 (A function should also be defined to do full *sh-style environment
221 variable expansion.)"""
226 while i < n
and path[i]
not in '/\\':
229 if not os.environ.has_key(
'HOME'):
231 userhome = os.environ[
'HOME']
234 return userhome + path[i:]
238 """Expand paths containing shell variable substitutions.
239 The following rules apply:
240 - no expansion within single quotes
241 - no escape character, except for '$$' which is translated into '$'
242 - ${varname} is accepted.
243 - varnames can be made out of letters, digits and the character '_'"""
250 varchars = string.ascii_letters + string.digits +
"_-"
254 while index < pathlen:
257 path = path[index + 1:]
260 index = path.index(
'\'')
261 res = res +
'\'' + path[:index + 1]
266 if path[index + 1:index + 2] ==
'$':
269 elif path[index + 1:index + 2] ==
'{':
270 path = path[index+2:]
273 index = path.index(
'}')
275 if os.environ.has_key(var):
276 res = res + os.environ[var]
283 c = path[index:index + 1]
284 while c !=
'' and c
in varchars:
287 c = path[index:index + 1]
288 if os.environ.has_key(var):
289 res = res + os.environ[var]
299 """Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
300 Also, components of the path are silently truncated to 8+3 notation."""
302 path = path.replace(
"/",
"\\")
304 while path[:1] ==
"\\":
305 prefix = prefix +
"\\"
307 comps = path.split(
"\\")
309 while i < len(comps):
312 elif comps[i] ==
'..' and i > 0
and \
313 comps[i-1]
not in (
'',
'..'):
316 elif comps[i] ==
'' and i > 0
and comps[i-1] !=
'':
318 elif '.' in comps[i]:
319 comp = comps[i].
split(
'.')
320 comps[i] = comp[0][:8] +
'.' + comp[1][:3]
322 elif len(comps[i]) > 8:
323 comps[i] = comps[i][:8]
328 if not prefix
and not comps:
330 return prefix +
"\\".
join(comps)
335 """Return an absolute path."""
337 path =
join(os.getcwd(), path)