blob: dc0aa108f0fcd1987bdc2d0ce096db397e7b73d7 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Common operations on Posix pathnames.
2
3Instead of importing this module directly, import os and refer to
4this module as os.path. The "os.path" name is an alias for this
5module on Posix systems; on other systems (e.g. Mac, Windows),
6os.path provides the same operations in a manner specific to that
7platform, and is an alias to another module (e.g. macpath, ntpath).
8
9Some of this can actually be useful on non-Posix systems too, e.g.
10for manipulation of the pathname component of URLs.
Guido van Rossum346f7af1997-12-05 19:04:51 +000011"""
Guido van Rossumc6360141990-10-13 19:23:40 +000012
Guido van Rossumd3876d31996-07-23 03:47:28 +000013import os
Guido van Rossum40d93041990-10-21 16:17:34 +000014import stat
Guido van Rossumd8faa362007-04-27 19:54:29 +000015import genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +000016from genericpath import *
Guido van Rossumc6360141990-10-13 19:23:40 +000017
Skip Montanaroc62c81e2001-02-12 02:00:42 +000018__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
19 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000020 "getatime","getctime","islink","exists","lexists","isdir","isfile",
Benjamin Petersond71ca412008-05-08 23:44:58 +000021 "ismount", "expanduser","expandvars","normpath","abspath",
Neal Norwitz61cdac62003-01-03 18:01:57 +000022 "samefile","sameopenfile","samestat",
Skip Montanaro117910d2003-02-14 19:35:31 +000023 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
Guido van Rossumd8faa362007-04-27 19:54:29 +000024 "devnull","realpath","supports_unicode_filenames","relpath"]
Guido van Rossumc6360141990-10-13 19:23:40 +000025
Skip Montanaro117910d2003-02-14 19:35:31 +000026# strings representing various path-related bits and pieces
27curdir = '.'
28pardir = '..'
29extsep = '.'
30sep = '/'
31pathsep = ':'
32defpath = ':/bin:/usr/bin'
33altsep = None
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000034devnull = '/dev/null'
Skip Montanaro117910d2003-02-14 19:35:31 +000035
Guido van Rossum7ac48781992-01-14 18:29:32 +000036# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
37# On MS-DOS this may also turn slashes into backslashes; however, other
38# normalizations (such as optimizing '../' away) are not allowed
39# (another function should be defined to do that).
40
41def normcase(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000042 """Normalize case of pathname. Has no effect under Posix"""
43 return s
Guido van Rossum7ac48781992-01-14 18:29:32 +000044
45
Jeremy Hyltona05e2932000-06-28 14:48:01 +000046# Return whether a path is absolute.
Guido van Rossum7ac48781992-01-14 18:29:32 +000047# Trivial in Posix, harder on the Mac or MS-DOS.
48
49def isabs(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000050 """Test whether a path is absolute"""
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000051 return s.startswith('/')
Guido van Rossum7ac48781992-01-14 18:29:32 +000052
53
Barry Warsaw384d2491997-02-18 21:53:25 +000054# Join pathnames.
55# Ignore the previous parts if a part is absolute.
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000056# Insert a '/' unless the first part is empty or already ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000057
Barry Warsaw384d2491997-02-18 21:53:25 +000058def join(a, *p):
Guido van Rossum04110fb2007-08-24 16:32:05 +000059 """Join two or more pathname components, inserting '/' as needed.
60 If any component is an absolute path, all previous path components
61 will be discarded."""
Guido van Rossum346f7af1997-12-05 19:04:51 +000062 path = a
63 for b in p:
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000064 if b.startswith('/'):
Guido van Rossum346f7af1997-12-05 19:04:51 +000065 path = b
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000066 elif path == '' or path.endswith('/'):
67 path += b
Guido van Rossum346f7af1997-12-05 19:04:51 +000068 else:
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000069 path += '/' + b
Guido van Rossum346f7af1997-12-05 19:04:51 +000070 return path
Guido van Rossumc6360141990-10-13 19:23:40 +000071
72
Guido van Rossum26847381992-03-31 18:54:35 +000073# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000074# rest). If the path ends in '/', tail will be empty. If there is no
75# '/' in the path, head will be empty.
76# Trailing '/'es are stripped from head unless it is the root.
Guido van Rossum7ac48781992-01-14 18:29:32 +000077
Guido van Rossumc6360141990-10-13 19:23:40 +000078def split(p):
Tim Peters2344fae2001-01-15 00:50:52 +000079 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
Fred Drakec0ab93e2000-09-28 16:22:52 +000080 everything after the final slash. Either part may be empty."""
Fred Drake22fb8392000-09-28 15:04:39 +000081 i = p.rfind('/') + 1
Guido van Rossum346f7af1997-12-05 19:04:51 +000082 head, tail = p[:i], p[i:]
Fred Drake8152d322000-12-12 23:20:45 +000083 if head and head != '/'*len(head):
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000084 head = head.rstrip('/')
Guido van Rossum346f7af1997-12-05 19:04:51 +000085 return head, tail
Guido van Rossumc6360141990-10-13 19:23:40 +000086
87
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000088# Split a path in root and extension.
Guido van Rossum422869a1996-08-20 20:24:17 +000089# The extension is everything starting at the last dot in the last
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000090# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +000091# It is always true that root + ext == p.
92
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000093def splitext(p):
Guido van Rossumd8faa362007-04-27 19:54:29 +000094 return genericpath._splitext(p, sep, altsep, extsep)
95splitext.__doc__ = genericpath._splitext.__doc__
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000096
Guido van Rossum221df241995-08-07 20:17:55 +000097# 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.
99
100def splitdrive(p):
Tim Peters2344fae2001-01-15 00:50:52 +0000101 """Split a pathname into drive and path. On Posix, drive is always
Fred Drakec0ab93e2000-09-28 16:22:52 +0000102 empty."""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000103 return '', p
Guido van Rossum221df241995-08-07 20:17:55 +0000104
105
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106# Return the tail (basename) part of a path, same as split(path)[1].
Guido van Rossum7ac48781992-01-14 18:29:32 +0000107
Guido van Rossumc6360141990-10-13 19:23:40 +0000108def basename(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000109 """Returns the final component of a pathname"""
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 i = p.rfind('/') + 1
111 return p[i:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000112
113
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114# Return the head (dirname) part of a path, same as split(path)[0].
Guido van Rossumc629d341992-11-05 10:43:02 +0000115
116def dirname(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000117 """Returns the directory component of a pathname"""
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 i = p.rfind('/') + 1
119 head = p[:i]
120 if head and head != '/'*len(head):
121 head = head.rstrip('/')
122 return head
Guido van Rossumc629d341992-11-05 10:43:02 +0000123
124
Guido van Rossum7ac48781992-01-14 18:29:32 +0000125# Is a path a symbolic link?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000126# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000127
128def islink(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000129 """Test whether a path is a symbolic link"""
130 try:
131 st = os.lstat(path)
132 except (os.error, AttributeError):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000133 return False
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000134 return stat.S_ISLNK(st.st_mode)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000135
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000136# Being true for dangling symbolic links is also useful.
137
138def lexists(path):
139 """Test whether a path exists. Returns True for broken symbolic links"""
140 try:
141 st = os.lstat(path)
142 except os.error:
143 return False
144 return True
145
146
Guido van Rossumd3778f91991-11-12 15:37:40 +0000147# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000148
Guido van Rossumd3778f91991-11-12 15:37:40 +0000149def samefile(f1, f2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000150 """Test whether two pathnames reference the same actual file"""
151 s1 = os.stat(f1)
152 s2 = os.stat(f2)
153 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000154
155
156# Are two open files really referencing the same file?
157# (Not necessarily the same file descriptor!)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000158
Guido van Rossumd3778f91991-11-12 15:37:40 +0000159def sameopenfile(fp1, fp2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000160 """Test whether two open file objects reference the same file"""
161 s1 = os.fstat(fp1)
162 s2 = os.fstat(fp2)
163 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000164
165
166# Are two stat buffers (obtained from stat, fstat or lstat)
167# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000168
Guido van Rossumd3778f91991-11-12 15:37:40 +0000169def samestat(s1, s2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000170 """Test whether two stat buffers reference the same file"""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000171 return s1.st_ino == s2.st_ino and \
172 s1.st_dev == s2.st_dev
Guido van Rossumc6360141990-10-13 19:23:40 +0000173
174
175# Is a path a mount point?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000176# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000177
Guido van Rossumc6360141990-10-13 19:23:40 +0000178def ismount(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000179 """Test whether a path is a mount point"""
180 try:
Christian Heimesfaf2f632008-01-06 16:59:19 +0000181 s1 = os.lstat(path)
182 s2 = os.lstat(join(path, '..'))
Guido van Rossum346f7af1997-12-05 19:04:51 +0000183 except os.error:
Tim Petersbc0e9102002-04-04 22:55:58 +0000184 return False # It doesn't exist -- so not a mount point :-)
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000185 dev1 = s1.st_dev
186 dev2 = s2.st_dev
Guido van Rossum346f7af1997-12-05 19:04:51 +0000187 if dev1 != dev2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000188 return True # path/.. on a different device as path
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000189 ino1 = s1.st_ino
190 ino2 = s2.st_ino
Guido van Rossum346f7af1997-12-05 19:04:51 +0000191 if ino1 == ino2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000192 return True # path/.. is the same i-node as path
193 return False
Guido van Rossumc6360141990-10-13 19:23:40 +0000194
195
Guido van Rossum7ac48781992-01-14 18:29:32 +0000196# Expand paths beginning with '~' or '~user'.
197# '~' means $HOME; '~user' means that user's home directory.
198# If the path doesn't begin with '~', or if the user or $HOME is unknown,
199# the path is returned unchanged (leaving error reporting to whatever
200# function is called with the expanded path as argument).
201# See also module 'glob' for expansion of *, ? and [...] in pathnames.
202# (A function should also be defined to do full *sh-style environment
203# variable expansion.)
204
205def expanduser(path):
Tim Peters2344fae2001-01-15 00:50:52 +0000206 """Expand ~ and ~user constructions. If user or $HOME is unknown,
Fred Drakec0ab93e2000-09-28 16:22:52 +0000207 do nothing."""
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000208 if not path.startswith('~'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000209 return path
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000210 i = path.find('/', 1)
211 if i < 0:
212 i = len(path)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000213 if i == 1:
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000214 if 'HOME' not in os.environ:
Neal Norwitz609ba812002-09-05 21:08:25 +0000215 import pwd
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000216 userhome = pwd.getpwuid(os.getuid()).pw_dir
Neal Norwitz609ba812002-09-05 21:08:25 +0000217 else:
218 userhome = os.environ['HOME']
Guido van Rossum346f7af1997-12-05 19:04:51 +0000219 else:
220 import pwd
221 try:
222 pwent = pwd.getpwnam(path[1:i])
223 except KeyError:
224 return path
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000225 userhome = pwent.pw_dir
Thomas Wouters89f507f2006-12-13 04:49:30 +0000226 userhome = userhome.rstrip('/')
Guido van Rossum346f7af1997-12-05 19:04:51 +0000227 return userhome + path[i:]
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000228
229
230# Expand paths containing shell variable substitutions.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231# This expands the forms $variable and ${variable} only.
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000232# Non-existent variables are left unchanged.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233
234_varprog = None
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000235
236def expandvars(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000237 """Expand shell variables of form $var and ${var}. Unknown variables
Fred Drakec0ab93e2000-09-28 16:22:52 +0000238 are left unchanged."""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000239 global _varprog
240 if '$' not in path:
241 return path
242 if not _varprog:
243 import re
244 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
245 i = 0
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000246 while True:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000247 m = _varprog.search(path, i)
248 if not m:
249 break
250 i, j = m.span(0)
251 name = m.group(1)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000252 if name.startswith('{') and name.endswith('}'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000253 name = name[1:-1]
Raymond Hettinger54f02222002-06-01 14:18:47 +0000254 if name in os.environ:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000255 tail = path[j:]
256 path = path[:i] + os.environ[name]
257 i = len(path)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000258 path += tail
Guido van Rossum346f7af1997-12-05 19:04:51 +0000259 else:
260 i = j
261 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000262
263
264# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
265# It should be understood that this may change the meaning of the path
266# if it contains symbolic links!
267
268def normpath(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000269 """Normalize path, eliminating double slashes, etc."""
Skip Montanaro018dfae2000-07-19 17:09:51 +0000270 if path == '':
271 return '.'
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000272 initial_slashes = path.startswith('/')
273 # POSIX allows one or two initial slashes, but treats three or more
274 # as single slash.
Tim Peters658cba62001-02-09 20:06:00 +0000275 if (initial_slashes and
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000276 path.startswith('//') and not path.startswith('///')):
277 initial_slashes = 2
Fred Drake22fb8392000-09-28 15:04:39 +0000278 comps = path.split('/')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000279 new_comps = []
280 for comp in comps:
281 if comp in ('', '.'):
282 continue
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000283 if (comp != '..' or (not initial_slashes and not new_comps) or
Skip Montanaro018dfae2000-07-19 17:09:51 +0000284 (new_comps and new_comps[-1] == '..')):
285 new_comps.append(comp)
286 elif new_comps:
287 new_comps.pop()
288 comps = new_comps
Fred Drake22fb8392000-09-28 15:04:39 +0000289 path = '/'.join(comps)
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000290 if initial_slashes:
291 path = '/'*initial_slashes + path
Skip Montanaro018dfae2000-07-19 17:09:51 +0000292 return path or '.'
Guido van Rossume294cf61999-01-29 18:05:18 +0000293
294
Guido van Rossume294cf61999-01-29 18:05:18 +0000295def abspath(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000296 """Return an absolute path."""
Guido van Rossume294cf61999-01-29 18:05:18 +0000297 if not isabs(path):
298 path = join(os.getcwd(), path)
299 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000300
301
302# Return a canonical path (i.e. the absolute location of a file on the
303# filesystem).
304
305def realpath(filename):
306 """Return the canonical path of the specified filename, eliminating any
307symbolic links encountered in the path."""
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000308 if isabs(filename):
309 bits = ['/'] + filename.split('/')[1:]
310 else:
Georg Brandl268e61c2005-06-03 14:28:50 +0000311 bits = [''] + filename.split('/')
Tim Petersa45cacf2004-08-20 03:47:14 +0000312
Guido van Rossum83eeef42001-09-17 15:16:09 +0000313 for i in range(2, len(bits)+1):
314 component = join(*bits[0:i])
Brett Cannonf50299c2004-07-10 22:55:15 +0000315 # Resolve symbolic links.
Brett Cannondfa5d952004-07-11 19:16:21 +0000316 if islink(component):
Brett Cannonf50299c2004-07-10 22:55:15 +0000317 resolved = _resolve_link(component)
318 if resolved is None:
319 # Infinite loop -- return original component + rest of the path
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000320 return abspath(join(*([component] + bits[i:])))
Brett Cannonf50299c2004-07-10 22:55:15 +0000321 else:
322 newpath = join(*([resolved] + bits[i:]))
Tim Petersa45cacf2004-08-20 03:47:14 +0000323 return realpath(newpath)
Tim Petersb64bec32001-09-18 02:26:39 +0000324
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000325 return abspath(filename)
Tim Petersa45cacf2004-08-20 03:47:14 +0000326
Brett Cannonf50299c2004-07-10 22:55:15 +0000327
328def _resolve_link(path):
329 """Internal helper function. Takes a path and follows symlinks
Tim Peters182b5ac2004-07-18 06:16:08 +0000330 until we either arrive at something that isn't a symlink, or
Brett Cannonf50299c2004-07-10 22:55:15 +0000331 encounter a path we've seen before (meaning that there's a loop).
332 """
333 paths_seen = []
334 while islink(path):
Brett Cannondfa5d952004-07-11 19:16:21 +0000335 if path in paths_seen:
Brett Cannonf50299c2004-07-10 22:55:15 +0000336 # Already seen this path, so we must have a symlink loop
337 return None
Brett Cannondfa5d952004-07-11 19:16:21 +0000338 paths_seen.append(path)
Brett Cannonf50299c2004-07-10 22:55:15 +0000339 # Resolve where the link points to
Brett Cannondfa5d952004-07-11 19:16:21 +0000340 resolved = os.readlink(path)
Andrew M. Kuchlingc75f1122004-08-02 14:54:16 +0000341 if not isabs(resolved):
Brett Cannonf50299c2004-07-10 22:55:15 +0000342 dir = dirname(path)
343 path = normpath(join(dir, resolved))
344 else:
345 path = normpath(resolved)
346 return path
347
Just van Rossum2d4e9882003-07-17 15:11:49 +0000348supports_unicode_filenames = False
Guido van Rossumd8faa362007-04-27 19:54:29 +0000349
350def relpath(path, start=curdir):
351 """Return a relative version of a path"""
352
353 if not path:
354 raise ValueError("no path specified")
355
356 start_list = abspath(start).split(sep)
357 path_list = abspath(path).split(sep)
358
359 # Work out how much of the filepath is shared by start and path.
360 i = len(commonprefix([start_list, path_list]))
361
362 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
Christian Heimesfaf2f632008-01-06 16:59:19 +0000363 if not rel_list:
364 return curdir
Guido van Rossumd8faa362007-04-27 19:54:29 +0000365 return join(*rel_list)