Patch by Jeremy Katz (SF #1609407)
[python.git] / Lib / posixpath.py
blob15212366057da308114bcf2e9d1fbf73478ca2ad
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
15 from genericpath import *
17 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
18 "basename","dirname","commonprefix","getsize","getmtime",
19 "getatime","getctime","islink","exists","lexists","isdir","isfile",
20 "ismount","walk","expanduser","expandvars","normpath","abspath",
21 "samefile","sameopenfile","samestat",
22 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
23 "devnull","realpath","supports_unicode_filenames"]
25 # strings representing various path-related bits and pieces
26 curdir = '.'
27 pardir = '..'
28 extsep = '.'
29 sep = '/'
30 pathsep = ':'
31 defpath = ':/bin:/usr/bin'
32 altsep = None
33 devnull = '/dev/null'
35 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
36 # On MS-DOS this may also turn slashes into backslashes; however, other
37 # normalizations (such as optimizing '../' away) are not allowed
38 # (another function should be defined to do that).
40 def normcase(s):
41 """Normalize case of pathname. Has no effect under Posix"""
42 return s
45 # Return whether a path is absolute.
46 # Trivial in Posix, harder on the Mac or MS-DOS.
48 def isabs(s):
49 """Test whether a path is absolute"""
50 return s.startswith('/')
53 # Join pathnames.
54 # Ignore the previous parts if a part is absolute.
55 # Insert a '/' unless the first part is empty or already ends in '/'.
57 def join(a, *p):
58 """Join two or more pathname components, inserting '/' as needed"""
59 path = a
60 for b in p:
61 if b.startswith('/'):
62 path = b
63 elif path == '' or path.endswith('/'):
64 path += b
65 else:
66 path += '/' + b
67 return path
70 # Split a path in head (everything up to the last '/') and tail (the
71 # rest). If the path ends in '/', tail will be empty. If there is no
72 # '/' in the path, head will be empty.
73 # Trailing '/'es are stripped from head unless it is the root.
75 def split(p):
76 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
77 everything after the final slash. Either part may be empty."""
78 i = p.rfind('/') + 1
79 head, tail = p[:i], p[i:]
80 if head and head != '/'*len(head):
81 head = head.rstrip('/')
82 return head, tail
85 # Split a path in root and extension.
86 # The extension is everything starting at the last dot in the last
87 # pathname component; the root is everything before that.
88 # It is always true that root + ext == p.
90 def splitext(p):
91 """Split the extension from a pathname. Extension is everything from the
92 last dot to the end. Returns "(root, ext)", either part may be empty."""
93 i = p.rfind('.')
94 if i<=p.rfind('/'):
95 return p, ''
96 else:
97 return p[:i], p[i:]
100 # Split a pathname into a drive specification and the rest of the
101 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
103 def splitdrive(p):
104 """Split a pathname into drive and path. On Posix, drive is always
105 empty."""
106 return '', p
109 # Return the tail (basename) part of a path, same as split(path)[1].
111 def basename(p):
112 """Returns the final component of a pathname"""
113 i = p.rfind('/') + 1
114 return p[i:]
117 # Return the head (dirname) part of a path, same as split(path)[0].
119 def dirname(p):
120 """Returns the directory component of a pathname"""
121 i = p.rfind('/') + 1
122 head = p[:i]
123 if head and head != '/'*len(head):
124 head = head.rstrip('/')
125 return head
128 # Is a path a symbolic link?
129 # This will always return false on systems where os.lstat doesn't exist.
131 def islink(path):
132 """Test whether a path is a symbolic link"""
133 try:
134 st = os.lstat(path)
135 except (os.error, AttributeError):
136 return False
137 return stat.S_ISLNK(st.st_mode)
139 # Being true for dangling symbolic links is also useful.
141 def lexists(path):
142 """Test whether a path exists. Returns True for broken symbolic links"""
143 try:
144 st = os.lstat(path)
145 except os.error:
146 return False
147 return True
150 # Are two filenames really pointing to the same file?
152 def samefile(f1, f2):
153 """Test whether two pathnames reference the same actual file"""
154 s1 = os.stat(f1)
155 s2 = os.stat(f2)
156 return samestat(s1, s2)
159 # Are two open files really referencing the same file?
160 # (Not necessarily the same file descriptor!)
162 def sameopenfile(fp1, fp2):
163 """Test whether two open file objects reference the same file"""
164 s1 = os.fstat(fp1)
165 s2 = os.fstat(fp2)
166 return samestat(s1, s2)
169 # Are two stat buffers (obtained from stat, fstat or lstat)
170 # describing the same file?
172 def samestat(s1, s2):
173 """Test whether two stat buffers reference the same file"""
174 return s1.st_ino == s2.st_ino and \
175 s1.st_dev == s2.st_dev
178 # Is a path a mount point?
179 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
181 def ismount(path):
182 """Test whether a path is a mount point"""
183 try:
184 s1 = os.stat(path)
185 s2 = os.stat(join(path, '..'))
186 except os.error:
187 return False # It doesn't exist -- so not a mount point :-)
188 dev1 = s1.st_dev
189 dev2 = s2.st_dev
190 if dev1 != dev2:
191 return True # path/.. on a different device as path
192 ino1 = s1.st_ino
193 ino2 = s2.st_ino
194 if ino1 == ino2:
195 return True # path/.. is the same i-node as path
196 return False
199 # Directory tree walk.
200 # For each directory under top (including top itself, but excluding
201 # '.' and '..'), func(arg, dirname, filenames) is called, where
202 # dirname is the name of the directory and filenames is the list
203 # of files (and subdirectories etc.) in the directory.
204 # The func may modify the filenames list, to implement a filter,
205 # or to impose a different order of visiting.
207 def walk(top, func, arg):
208 """Directory tree walk with callback function.
210 For each directory in the directory tree rooted at top (including top
211 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
212 dirname is the name of the directory, and fnames a list of the names of
213 the files and subdirectories in dirname (excluding '.' and '..'). func
214 may modify the fnames list in-place (e.g. via del or slice assignment),
215 and walk will only recurse into the subdirectories whose names remain in
216 fnames; this can be used to implement a filter, or to impose a specific
217 order of visiting. No semantics are defined for, or required of, arg,
218 beyond that arg is always passed to func. It can be used, e.g., to pass
219 a filename pattern, or a mutable object designed to accumulate
220 statistics. Passing None for arg is common."""
222 try:
223 names = os.listdir(top)
224 except os.error:
225 return
226 func(arg, top, names)
227 for name in names:
228 name = join(top, name)
229 try:
230 st = os.lstat(name)
231 except os.error:
232 continue
233 if stat.S_ISDIR(st.st_mode):
234 walk(name, func, arg)
237 # Expand paths beginning with '~' or '~user'.
238 # '~' means $HOME; '~user' means that user's home directory.
239 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
240 # the path is returned unchanged (leaving error reporting to whatever
241 # function is called with the expanded path as argument).
242 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
243 # (A function should also be defined to do full *sh-style environment
244 # variable expansion.)
246 def expanduser(path):
247 """Expand ~ and ~user constructions. If user or $HOME is unknown,
248 do nothing."""
249 if not path.startswith('~'):
250 return path
251 i = path.find('/', 1)
252 if i < 0:
253 i = len(path)
254 if i == 1:
255 if 'HOME' not in os.environ:
256 import pwd
257 userhome = pwd.getpwuid(os.getuid()).pw_dir
258 else:
259 userhome = os.environ['HOME']
260 else:
261 import pwd
262 try:
263 pwent = pwd.getpwnam(path[1:i])
264 except KeyError:
265 return path
266 userhome = pwent.pw_dir
267 userhome = userhome.rstrip('/')
268 return userhome + path[i:]
271 # Expand paths containing shell variable substitutions.
272 # This expands the forms $variable and ${variable} only.
273 # Non-existent variables are left unchanged.
275 _varprog = None
277 def expandvars(path):
278 """Expand shell variables of form $var and ${var}. Unknown variables
279 are left unchanged."""
280 global _varprog
281 if '$' not in path:
282 return path
283 if not _varprog:
284 import re
285 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
286 i = 0
287 while True:
288 m = _varprog.search(path, i)
289 if not m:
290 break
291 i, j = m.span(0)
292 name = m.group(1)
293 if name.startswith('{') and name.endswith('}'):
294 name = name[1:-1]
295 if name in os.environ:
296 tail = path[j:]
297 path = path[:i] + os.environ[name]
298 i = len(path)
299 path += tail
300 else:
301 i = j
302 return path
305 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
306 # It should be understood that this may change the meaning of the path
307 # if it contains symbolic links!
309 def normpath(path):
310 """Normalize path, eliminating double slashes, etc."""
311 if path == '':
312 return '.'
313 initial_slashes = path.startswith('/')
314 # POSIX allows one or two initial slashes, but treats three or more
315 # as single slash.
316 if (initial_slashes and
317 path.startswith('//') and not path.startswith('///')):
318 initial_slashes = 2
319 comps = path.split('/')
320 new_comps = []
321 for comp in comps:
322 if comp in ('', '.'):
323 continue
324 if (comp != '..' or (not initial_slashes and not new_comps) or
325 (new_comps and new_comps[-1] == '..')):
326 new_comps.append(comp)
327 elif new_comps:
328 new_comps.pop()
329 comps = new_comps
330 path = '/'.join(comps)
331 if initial_slashes:
332 path = '/'*initial_slashes + path
333 return path or '.'
336 def abspath(path):
337 """Return an absolute path."""
338 if not isabs(path):
339 path = join(os.getcwd(), path)
340 return normpath(path)
343 # Return a canonical path (i.e. the absolute location of a file on the
344 # filesystem).
346 def realpath(filename):
347 """Return the canonical path of the specified filename, eliminating any
348 symbolic links encountered in the path."""
349 if isabs(filename):
350 bits = ['/'] + filename.split('/')[1:]
351 else:
352 bits = [''] + filename.split('/')
354 for i in range(2, len(bits)+1):
355 component = join(*bits[0:i])
356 # Resolve symbolic links.
357 if islink(component):
358 resolved = _resolve_link(component)
359 if resolved is None:
360 # Infinite loop -- return original component + rest of the path
361 return abspath(join(*([component] + bits[i:])))
362 else:
363 newpath = join(*([resolved] + bits[i:]))
364 return realpath(newpath)
366 return abspath(filename)
369 def _resolve_link(path):
370 """Internal helper function. Takes a path and follows symlinks
371 until we either arrive at something that isn't a symlink, or
372 encounter a path we've seen before (meaning that there's a loop).
374 paths_seen = []
375 while islink(path):
376 if path in paths_seen:
377 # Already seen this path, so we must have a symlink loop
378 return None
379 paths_seen.append(path)
380 # Resolve where the link points to
381 resolved = os.readlink(path)
382 if not isabs(resolved):
383 dir = dirname(path)
384 path = normpath(join(dir, resolved))
385 else:
386 path = normpath(resolved)
387 return path
389 supports_unicode_filenames = False