append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Lib / posixpath.py
blobbf153906b4769c750b7eb51564cccab69fb5a707
1 """Common operations on Posix pathnames.
3 Instead of importing this module directly, import os and refer to
4 this module as os.path. The "os.path" name is an alias for this
5 module on Posix systems; on other systems (e.g. Mac, Windows),
6 os.path provides the same operations in a manner specific to that
7 platform, and is an alias to another module (e.g. macpath, ntpath).
9 Some of this can actually be useful on non-Posix systems too, e.g.
10 for manipulation of the pathname component of URLs.
11 """
13 import os
14 import stat
16 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
17 "basename","dirname","commonprefix","getsize","getmtime",
18 "getatime","islink","exists","isdir","isfile","ismount",
19 "walk","expanduser","expandvars","normpath","abspath",
20 "samefile","sameopenfile","samestat","supports_unicode_filenames"]
22 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
23 # On MS-DOS this may also turn slashes into backslashes; however, other
24 # normalizations (such as optimizing '../' away) are not allowed
25 # (another function should be defined to do that).
27 def normcase(s):
28 """Normalize case of pathname. Has no effect under Posix"""
29 return s
32 # Return whether a path is absolute.
33 # Trivial in Posix, harder on the Mac or MS-DOS.
35 def isabs(s):
36 """Test whether a path is absolute"""
37 return s[:1] == '/'
40 # Join pathnames.
41 # Ignore the previous parts if a part is absolute.
42 # Insert a '/' unless the first part is empty or already ends in '/'.
44 def join(a, *p):
45 """Join two or more pathname components, inserting '/' as needed"""
46 path = a
47 for b in p:
48 if b[:1] == '/':
49 path = b
50 elif path == '' or path[-1:] == '/':
51 path = path + b
52 else:
53 path = path + '/' + b
54 return path
57 # Split a path in head (everything up to the last '/') and tail (the
58 # rest). If the path ends in '/', tail will be empty. If there is no
59 # '/' in the path, head will be empty.
60 # Trailing '/'es are stripped from head unless it is the root.
62 def split(p):
63 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
64 everything after the final slash. Either part may be empty."""
65 i = p.rfind('/') + 1
66 head, tail = p[:i], p[i:]
67 if head and head != '/'*len(head):
68 while head[-1] == '/':
69 head = head[:-1]
70 return head, tail
73 # Split a path in root and extension.
74 # The extension is everything starting at the last dot in the last
75 # pathname component; the root is everything before that.
76 # It is always true that root + ext == p.
78 def splitext(p):
79 """Split the extension from a pathname. Extension is everything from the
80 last dot to the end. Returns "(root, ext)", either part may be empty."""
81 root, ext = '', ''
82 for c in p:
83 if c == '/':
84 root, ext = root + ext + c, ''
85 elif c == '.':
86 if ext:
87 root, ext = root + ext, c
88 else:
89 ext = c
90 elif ext:
91 ext = ext + c
92 else:
93 root = root + c
94 return root, ext
97 # Split a pathname into a drive specification and the rest of the
98 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
100 def splitdrive(p):
101 """Split a pathname into drive and path. On Posix, drive is always
102 empty."""
103 return '', p
106 # Return the tail (basename) part of a path.
108 def basename(p):
109 """Returns the final component of a pathname"""
110 return split(p)[1]
113 # Return the head (dirname) part of a path.
115 def dirname(p):
116 """Returns the directory component of a pathname"""
117 return split(p)[0]
120 # Return the longest prefix of all list elements.
122 def commonprefix(m):
123 "Given a list of pathnames, returns the longest common leading component"
124 if not m: return ''
125 prefix = m[0]
126 for item in m:
127 for i in range(len(prefix)):
128 if prefix[:i+1] != item[:i+1]:
129 prefix = prefix[:i]
130 if i == 0: return ''
131 break
132 return prefix
135 # Get size, mtime, atime of files.
137 def getsize(filename):
138 """Return the size of a file, reported by os.stat()."""
139 return os.stat(filename).st_size
141 def getmtime(filename):
142 """Return the last modification time of a file, reported by os.stat()."""
143 return os.stat(filename).st_mtime
145 def getatime(filename):
146 """Return the last access time of a file, reported by os.stat()."""
147 return os.stat(filename).st_atime
150 # Is a path a symbolic link?
151 # This will always return false on systems where os.lstat doesn't exist.
153 def islink(path):
154 """Test whether a path is a symbolic link"""
155 try:
156 st = os.lstat(path)
157 except (os.error, AttributeError):
158 return False
159 return stat.S_ISLNK(st.st_mode)
162 # Does a path exist?
163 # This is false for dangling symbolic links.
165 def exists(path):
166 """Test whether a path exists. Returns False for broken symbolic links"""
167 try:
168 st = os.stat(path)
169 except os.error:
170 return False
171 return True
174 # Is a path a directory?
175 # This follows symbolic links, so both islink() and isdir() can be true
176 # for the same path.
178 def isdir(path):
179 """Test whether a path is a directory"""
180 try:
181 st = os.stat(path)
182 except os.error:
183 return False
184 return stat.S_ISDIR(st.st_mode)
187 # Is a path a regular file?
188 # This follows symbolic links, so both islink() and isfile() can be true
189 # for the same path.
191 def isfile(path):
192 """Test whether a path is a regular file"""
193 try:
194 st = os.stat(path)
195 except os.error:
196 return False
197 return stat.S_ISREG(st.st_mode)
200 # Are two filenames really pointing to the same file?
202 def samefile(f1, f2):
203 """Test whether two pathnames reference the same actual file"""
204 s1 = os.stat(f1)
205 s2 = os.stat(f2)
206 return samestat(s1, s2)
209 # Are two open files really referencing the same file?
210 # (Not necessarily the same file descriptor!)
212 def sameopenfile(fp1, fp2):
213 """Test whether two open file objects reference the same file"""
214 s1 = os.fstat(fp1)
215 s2 = os.fstat(fp2)
216 return samestat(s1, s2)
219 # Are two stat buffers (obtained from stat, fstat or lstat)
220 # describing the same file?
222 def samestat(s1, s2):
223 """Test whether two stat buffers reference the same file"""
224 return s1.st_ino == s2.st_ino and \
225 s1.st_dev == s2.st_dev
228 # Is a path a mount point?
229 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
231 def ismount(path):
232 """Test whether a path is a mount point"""
233 try:
234 s1 = os.stat(path)
235 s2 = os.stat(join(path, '..'))
236 except os.error:
237 return False # It doesn't exist -- so not a mount point :-)
238 dev1 = s1.st_dev
239 dev2 = s2.st_dev
240 if dev1 != dev2:
241 return True # path/.. on a different device as path
242 ino1 = s1.st_ino
243 ino2 = s2.st_ino
244 if ino1 == ino2:
245 return True # path/.. is the same i-node as path
246 return False
249 # Directory tree walk.
250 # For each directory under top (including top itself, but excluding
251 # '.' and '..'), func(arg, dirname, filenames) is called, where
252 # dirname is the name of the directory and filenames is the list
253 # of files (and subdirectories etc.) in the directory.
254 # The func may modify the filenames list, to implement a filter,
255 # or to impose a different order of visiting.
257 def walk(top, func, arg):
258 """Directory tree walk with callback function.
260 For each directory in the directory tree rooted at top (including top
261 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
262 dirname is the name of the directory, and fnames a list of the names of
263 the files and subdirectories in dirname (excluding '.' and '..'). func
264 may modify the fnames list in-place (e.g. via del or slice assignment),
265 and walk will only recurse into the subdirectories whose names remain in
266 fnames; this can be used to implement a filter, or to impose a specific
267 order of visiting. No semantics are defined for, or required of, arg,
268 beyond that arg is always passed to func. It can be used, e.g., to pass
269 a filename pattern, or a mutable object designed to accumulate
270 statistics. Passing None for arg is common."""
272 try:
273 names = os.listdir(top)
274 except os.error:
275 return
276 func(arg, top, names)
277 for name in names:
278 name = join(top, name)
279 try:
280 st = os.lstat(name)
281 except os.error:
282 continue
283 if stat.S_ISDIR(st.st_mode):
284 walk(name, func, arg)
287 # Expand paths beginning with '~' or '~user'.
288 # '~' means $HOME; '~user' means that user's home directory.
289 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
290 # the path is returned unchanged (leaving error reporting to whatever
291 # function is called with the expanded path as argument).
292 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
293 # (A function should also be defined to do full *sh-style environment
294 # variable expansion.)
296 def expanduser(path):
297 """Expand ~ and ~user constructions. If user or $HOME is unknown,
298 do nothing."""
299 if path[:1] != '~':
300 return path
301 i, n = 1, len(path)
302 while i < n and path[i] != '/':
303 i = i + 1
304 if i == 1:
305 if not 'HOME' in os.environ:
306 import pwd
307 userhome = pwd.getpwuid(os.getuid())[5]
308 else:
309 userhome = os.environ['HOME']
310 else:
311 import pwd
312 try:
313 pwent = pwd.getpwnam(path[1:i])
314 except KeyError:
315 return path
316 userhome = pwent[5]
317 if userhome[-1:] == '/': i = i + 1
318 return userhome + path[i:]
321 # Expand paths containing shell variable substitutions.
322 # This expands the forms $variable and ${variable} only.
323 # Non-existent variables are left unchanged.
325 _varprog = None
327 def expandvars(path):
328 """Expand shell variables of form $var and ${var}. Unknown variables
329 are left unchanged."""
330 global _varprog
331 if '$' not in path:
332 return path
333 if not _varprog:
334 import re
335 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
336 i = 0
337 while True:
338 m = _varprog.search(path, i)
339 if not m:
340 break
341 i, j = m.span(0)
342 name = m.group(1)
343 if name[:1] == '{' and name[-1:] == '}':
344 name = name[1:-1]
345 if name in os.environ:
346 tail = path[j:]
347 path = path[:i] + os.environ[name]
348 i = len(path)
349 path = path + tail
350 else:
351 i = j
352 return path
355 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
356 # It should be understood that this may change the meaning of the path
357 # if it contains symbolic links!
359 def normpath(path):
360 """Normalize path, eliminating double slashes, etc."""
361 if path == '':
362 return '.'
363 initial_slashes = path.startswith('/')
364 # POSIX allows one or two initial slashes, but treats three or more
365 # as single slash.
366 if (initial_slashes and
367 path.startswith('//') and not path.startswith('///')):
368 initial_slashes = 2
369 comps = path.split('/')
370 new_comps = []
371 for comp in comps:
372 if comp in ('', '.'):
373 continue
374 if (comp != '..' or (not initial_slashes and not new_comps) or
375 (new_comps and new_comps[-1] == '..')):
376 new_comps.append(comp)
377 elif new_comps:
378 new_comps.pop()
379 comps = new_comps
380 path = '/'.join(comps)
381 if initial_slashes:
382 path = '/'*initial_slashes + path
383 return path or '.'
386 def abspath(path):
387 """Return an absolute path."""
388 if not isabs(path):
389 path = join(os.getcwd(), path)
390 return normpath(path)
393 # Return a canonical path (i.e. the absolute location of a file on the
394 # filesystem).
396 def realpath(filename):
397 """Return the canonical path of the specified filename, eliminating any
398 symbolic links encountered in the path."""
399 filename = abspath(filename)
401 bits = ['/'] + filename.split('/')[1:]
402 for i in range(2, len(bits)+1):
403 component = join(*bits[0:i])
404 if islink(component):
405 resolved = os.readlink(component)
406 (dir, file) = split(component)
407 resolved = normpath(join(dir, resolved))
408 newpath = join(*([resolved] + bits[i:]))
409 return realpath(newpath)
411 return filename
413 supports_unicode_filenames = False