blob: 32850b4ba74589cc9aec769751ebeb9b9934697c [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 Rossumc6360141990-10-13 19:23:40 +000015
16
Guido van Rossum7ac48781992-01-14 18:29:32 +000017# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
18# On MS-DOS this may also turn slashes into backslashes; however, other
19# normalizations (such as optimizing '../' away) are not allowed
20# (another function should be defined to do that).
21
22def normcase(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000023 """Normalize case of pathname. Has no effect under Posix"""
24 return s
Guido van Rossum7ac48781992-01-14 18:29:32 +000025
26
Jeremy Hyltona05e2932000-06-28 14:48:01 +000027# Return whether a path is absolute.
Guido van Rossum7ac48781992-01-14 18:29:32 +000028# Trivial in Posix, harder on the Mac or MS-DOS.
29
30def isabs(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000031 """Test whether a path is absolute"""
32 return s[:1] == '/'
Guido van Rossum7ac48781992-01-14 18:29:32 +000033
34
Barry Warsaw384d2491997-02-18 21:53:25 +000035# Join pathnames.
36# Ignore the previous parts if a part is absolute.
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000037# Insert a '/' unless the first part is empty or already ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000038
Barry Warsaw384d2491997-02-18 21:53:25 +000039def join(a, *p):
Guido van Rossum346f7af1997-12-05 19:04:51 +000040 """Join two or more pathname components, inserting '/' as needed"""
41 path = a
42 for b in p:
43 if b[:1] == '/':
44 path = b
45 elif path == '' or path[-1:] == '/':
46 path = path + b
47 else:
48 path = path + '/' + b
49 return path
Guido van Rossumc6360141990-10-13 19:23:40 +000050
51
Guido van Rossum26847381992-03-31 18:54:35 +000052# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000053# rest). If the path ends in '/', tail will be empty. If there is no
54# '/' in the path, head will be empty.
55# Trailing '/'es are stripped from head unless it is the root.
Guido van Rossum7ac48781992-01-14 18:29:32 +000056
Guido van Rossumc6360141990-10-13 19:23:40 +000057def split(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +000058 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
Fred Drakec0ab93e2000-09-28 16:22:52 +000059 everything after the final slash. Either part may be empty."""
Fred Drake22fb8392000-09-28 15:04:39 +000060 i = p.rfind('/') + 1
Guido van Rossum346f7af1997-12-05 19:04:51 +000061 head, tail = p[:i], p[i:]
Fred Drake8152d322000-12-12 23:20:45 +000062 if head and head != '/'*len(head):
Guido van Rossum346f7af1997-12-05 19:04:51 +000063 while head[-1] == '/':
64 head = head[:-1]
65 return head, tail
Guido van Rossumc6360141990-10-13 19:23:40 +000066
67
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000068# Split a path in root and extension.
Guido van Rossum422869a1996-08-20 20:24:17 +000069# The extension is everything starting at the last dot in the last
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000070# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +000071# It is always true that root + ext == p.
72
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000073def splitext(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +000074 """Split the extension from a pathname. Extension is everything from the
Fred Drakec0ab93e2000-09-28 16:22:52 +000075 last dot to the end. Returns "(root, ext)", either part may be empty."""
Guido van Rossum346f7af1997-12-05 19:04:51 +000076 root, ext = '', ''
77 for c in p:
78 if c == '/':
79 root, ext = root + ext + c, ''
80 elif c == '.':
81 if ext:
82 root, ext = root + ext, c
83 else:
84 ext = c
85 elif ext:
86 ext = ext + c
87 else:
88 root = root + c
89 return root, ext
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000090
91
Guido van Rossum221df241995-08-07 20:17:55 +000092# Split a pathname into a drive specification and the rest of the
93# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
94
95def splitdrive(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +000096 """Split a pathname into drive and path. On Posix, drive is always
Fred Drakec0ab93e2000-09-28 16:22:52 +000097 empty."""
Guido van Rossum346f7af1997-12-05 19:04:51 +000098 return '', p
Guido van Rossum221df241995-08-07 20:17:55 +000099
100
Guido van Rossumc6360141990-10-13 19:23:40 +0000101# Return the tail (basename) part of a path.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000102
Guido van Rossumc6360141990-10-13 19:23:40 +0000103def basename(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000104 """Returns the final component of a pathname"""
105 return split(p)[1]
Guido van Rossumc6360141990-10-13 19:23:40 +0000106
107
Guido van Rossumc629d341992-11-05 10:43:02 +0000108# Return the head (dirname) part of a path.
109
110def dirname(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000111 """Returns the directory component of a pathname"""
112 return split(p)[0]
Guido van Rossumc629d341992-11-05 10:43:02 +0000113
114
Guido van Rossumc6360141990-10-13 19:23:40 +0000115# Return the longest prefix of all list elements.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000116
Guido van Rossumc6360141990-10-13 19:23:40 +0000117def commonprefix(m):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000118 "Given a list of pathnames, returns the longest common leading component"
119 if not m: return ''
Skip Montanaro62358312000-08-22 13:01:53 +0000120 prefix = m[0]
121 for item in m:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000122 for i in range(len(prefix)):
Fred Drake8152d322000-12-12 23:20:45 +0000123 if prefix[:i+1] != item[:i+1]:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000124 prefix = prefix[:i]
125 if i == 0: return ''
126 break
Skip Montanaro62358312000-08-22 13:01:53 +0000127 return prefix
Guido van Rossumc6360141990-10-13 19:23:40 +0000128
129
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000130# Get size, mtime, atime of files.
131
132def getsize(filename):
133 """Return the size of a file, reported by os.stat()."""
134 st = os.stat(filename)
135 return st[stat.ST_SIZE]
136
137def getmtime(filename):
138 """Return the last modification time of a file, reported by os.stat()."""
139 st = os.stat(filename)
140 return st[stat.ST_MTIME]
141
142def getatime(filename):
143 """Return the last access time of a file, reported by os.stat()."""
144 st = os.stat(filename)
Guido van Rossum98118612000-02-24 02:26:51 +0000145 return st[stat.ST_ATIME]
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000146
147
Guido van Rossum7ac48781992-01-14 18:29:32 +0000148# Is a path a symbolic link?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000149# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000150
151def islink(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000152 """Test whether a path is a symbolic link"""
153 try:
154 st = os.lstat(path)
155 except (os.error, AttributeError):
156 return 0
157 return stat.S_ISLNK(st[stat.ST_MODE])
Guido van Rossum7ac48781992-01-14 18:29:32 +0000158
159
160# Does a path exist?
161# This is false for dangling symbolic links.
162
Guido van Rossumc6360141990-10-13 19:23:40 +0000163def exists(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000164 """Test whether a path exists. Returns false for broken symbolic links"""
165 try:
166 st = os.stat(path)
167 except os.error:
168 return 0
169 return 1
Guido van Rossumc6360141990-10-13 19:23:40 +0000170
171
Guido van Rossumd3876d31996-07-23 03:47:28 +0000172# Is a path a directory?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000173# This follows symbolic links, so both islink() and isdir() can be true
174# for the same path.
175
Guido van Rossumc6360141990-10-13 19:23:40 +0000176def isdir(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000177 """Test whether a path is a directory"""
178 try:
179 st = os.stat(path)
180 except os.error:
181 return 0
182 return stat.S_ISDIR(st[stat.ST_MODE])
Guido van Rossumc6360141990-10-13 19:23:40 +0000183
184
Guido van Rossum26847381992-03-31 18:54:35 +0000185# Is a path a regular file?
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186# This follows symbolic links, so both islink() and isfile() can be true
Guido van Rossum7ac48781992-01-14 18:29:32 +0000187# for the same path.
188
189def isfile(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000190 """Test whether a path is a regular file"""
191 try:
192 st = os.stat(path)
193 except os.error:
194 return 0
195 return stat.S_ISREG(st[stat.ST_MODE])
Guido van Rossumc6360141990-10-13 19:23:40 +0000196
197
Guido van Rossumd3778f91991-11-12 15:37:40 +0000198# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000199
Guido van Rossumd3778f91991-11-12 15:37:40 +0000200def samefile(f1, f2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000201 """Test whether two pathnames reference the same actual file"""
202 s1 = os.stat(f1)
203 s2 = os.stat(f2)
204 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000205
206
207# Are two open files really referencing the same file?
208# (Not necessarily the same file descriptor!)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000209
Guido van Rossumd3778f91991-11-12 15:37:40 +0000210def sameopenfile(fp1, fp2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000211 """Test whether two open file objects reference the same file"""
212 s1 = os.fstat(fp1)
213 s2 = os.fstat(fp2)
214 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000215
216
217# Are two stat buffers (obtained from stat, fstat or lstat)
218# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000219
Guido van Rossumd3778f91991-11-12 15:37:40 +0000220def samestat(s1, s2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000221 """Test whether two stat buffers reference the same file"""
222 return s1[stat.ST_INO] == s2[stat.ST_INO] and \
223 s1[stat.ST_DEV] == s2[stat.ST_DEV]
Guido van Rossumc6360141990-10-13 19:23:40 +0000224
225
226# Is a path a mount point?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000227# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000228
Guido van Rossumc6360141990-10-13 19:23:40 +0000229def ismount(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000230 """Test whether a path is a mount point"""
231 try:
232 s1 = os.stat(path)
233 s2 = os.stat(join(path, '..'))
234 except os.error:
235 return 0 # It doesn't exist -- so not a mount point :-)
236 dev1 = s1[stat.ST_DEV]
237 dev2 = s2[stat.ST_DEV]
238 if dev1 != dev2:
239 return 1 # path/.. on a different device as path
240 ino1 = s1[stat.ST_INO]
241 ino2 = s2[stat.ST_INO]
242 if ino1 == ino2:
243 return 1 # path/.. is the same i-node as path
244 return 0
Guido van Rossumc6360141990-10-13 19:23:40 +0000245
246
247# Directory tree walk.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000248# For each directory under top (including top itself, but excluding
249# '.' and '..'), func(arg, dirname, filenames) is called, where
250# dirname is the name of the directory and filenames is the list
Guido van Rossum346f7af1997-12-05 19:04:51 +0000251# of files (and subdirectories etc.) in the directory.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000252# The func may modify the filenames list, to implement a filter,
Guido van Rossumc6360141990-10-13 19:23:40 +0000253# or to impose a different order of visiting.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000254
Guido van Rossumc6360141990-10-13 19:23:40 +0000255def walk(top, func, arg):
Guido van Rossumf618a481999-11-02 13:29:08 +0000256 """walk(top,func,arg) calls func(arg, d, files) for each directory "d"
Fred Drakec0ab93e2000-09-28 16:22:52 +0000257 in the tree rooted at "top" (including "top" itself). "files" is a list
258 of all the files and subdirs in directory "d".
259 """
Guido van Rossum346f7af1997-12-05 19:04:51 +0000260 try:
261 names = os.listdir(top)
262 except os.error:
263 return
264 func(arg, top, names)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000265 for name in names:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000266 name = join(top, name)
Guido van Rossum84a74592000-02-28 14:27:07 +0000267 st = os.lstat(name)
268 if stat.S_ISDIR(st[stat.ST_MODE]):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000269 walk(name, func, arg)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000270
271
272# Expand paths beginning with '~' or '~user'.
273# '~' means $HOME; '~user' means that user's home directory.
274# If the path doesn't begin with '~', or if the user or $HOME is unknown,
275# the path is returned unchanged (leaving error reporting to whatever
276# function is called with the expanded path as argument).
277# See also module 'glob' for expansion of *, ? and [...] in pathnames.
278# (A function should also be defined to do full *sh-style environment
279# variable expansion.)
280
281def expanduser(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000282 """Expand ~ and ~user constructions. If user or $HOME is unknown,
Fred Drakec0ab93e2000-09-28 16:22:52 +0000283 do nothing."""
Fred Drake8152d322000-12-12 23:20:45 +0000284 if path[:1] != '~':
Guido van Rossum346f7af1997-12-05 19:04:51 +0000285 return path
286 i, n = 1, len(path)
Fred Drake8152d322000-12-12 23:20:45 +0000287 while i < n and path[i] != '/':
Fred Drakec0ab93e2000-09-28 16:22:52 +0000288 i = i + 1
Guido van Rossum346f7af1997-12-05 19:04:51 +0000289 if i == 1:
290 if not os.environ.has_key('HOME'):
291 return path
292 userhome = os.environ['HOME']
293 else:
294 import pwd
295 try:
296 pwent = pwd.getpwnam(path[1:i])
297 except KeyError:
298 return path
299 userhome = pwent[5]
Fred Drakec0ab93e2000-09-28 16:22:52 +0000300 if userhome[-1:] == '/': i = i + 1
Guido van Rossum346f7af1997-12-05 19:04:51 +0000301 return userhome + path[i:]
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000302
303
304# Expand paths containing shell variable substitutions.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305# This expands the forms $variable and ${variable} only.
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000306# Non-existent variables are left unchanged.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307
308_varprog = None
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000309
310def expandvars(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000311 """Expand shell variables of form $var and ${var}. Unknown variables
Fred Drakec0ab93e2000-09-28 16:22:52 +0000312 are left unchanged."""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000313 global _varprog
314 if '$' not in path:
315 return path
316 if not _varprog:
317 import re
318 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
319 i = 0
320 while 1:
321 m = _varprog.search(path, i)
322 if not m:
323 break
324 i, j = m.span(0)
325 name = m.group(1)
326 if name[:1] == '{' and name[-1:] == '}':
327 name = name[1:-1]
328 if os.environ.has_key(name):
329 tail = path[j:]
330 path = path[:i] + os.environ[name]
331 i = len(path)
332 path = path + tail
333 else:
334 i = j
335 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000336
337
338# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
339# It should be understood that this may change the meaning of the path
340# if it contains symbolic links!
341
342def normpath(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000343 """Normalize path, eliminating double slashes, etc."""
Skip Montanaro018dfae2000-07-19 17:09:51 +0000344 if path == '':
345 return '.'
Skip Montanaro018dfae2000-07-19 17:09:51 +0000346 initial_slash = (path[0] == '/')
Fred Drake22fb8392000-09-28 15:04:39 +0000347 comps = path.split('/')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000348 new_comps = []
349 for comp in comps:
350 if comp in ('', '.'):
351 continue
352 if (comp != '..' or (not initial_slash and not new_comps) or
353 (new_comps and new_comps[-1] == '..')):
354 new_comps.append(comp)
355 elif new_comps:
356 new_comps.pop()
357 comps = new_comps
Fred Drake22fb8392000-09-28 15:04:39 +0000358 path = '/'.join(comps)
Skip Montanaro018dfae2000-07-19 17:09:51 +0000359 if initial_slash:
360 path = '/' + path
361 return path or '.'
Guido van Rossume294cf61999-01-29 18:05:18 +0000362
363
Guido van Rossume294cf61999-01-29 18:05:18 +0000364def abspath(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000365 """Return an absolute path."""
Guido van Rossume294cf61999-01-29 18:05:18 +0000366 if not isabs(path):
367 path = join(os.getcwd(), path)
368 return normpath(path)