blob: 14b9270fba37abf07592dd5d0787453ac1c77565 [file] [log] [blame]
Guido van Rossumaad67612000-05-08 17:31:04 +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 Rossumb978d181997-12-09 16:56:41 +000011"""
Guido van Rossum5c971671996-07-22 15:23:25 +000012
Guido van Rossum3bb710d1996-07-30 16:35:26 +000013import os
Guido van Rossum5c971671996-07-22 15:23:25 +000014import stat
15
16
17# 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 Rossumb978d181997-12-09 16:56:41 +000023 """Normalize case of pathname. Has no effect under Posix"""
24 return s
Guido van Rossum5c971671996-07-22 15:23:25 +000025
26
Jeremy Hyltona05e2932000-06-28 14:48:01 +000027# Return whether a path is absolute.
Guido van Rossum5c971671996-07-22 15:23:25 +000028# Trivial in Posix, harder on the Mac or MS-DOS.
29
30def isabs(s):
Guido van Rossumb978d181997-12-09 16:56:41 +000031 """Test whether a path is absolute"""
32 return s[:1] == '/'
Guido van Rossum5c971671996-07-22 15:23:25 +000033
34
Guido van Rossum228b8e81997-04-02 06:13:34 +000035# Join pathnames.
36# Ignore the previous parts if a part is absolute.
Guido van Rossum5c971671996-07-22 15:23:25 +000037# Insert a '/' unless the first part is empty or already ends in '/'.
38
Guido van Rossum228b8e81997-04-02 06:13:34 +000039def join(a, *p):
Guido van Rossumb978d181997-12-09 16:56:41 +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 Rossum5c971671996-07-22 15:23:25 +000050
51
52# Split a path in head (everything up to the last '/') and tail (the
53# 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.
56
57def split(p):
Guido van Rossumb978d181997-12-09 16:56:41 +000058 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
59everything after the final slash. Either part may be empty"""
60 import string
61 i = string.rfind(p, '/') + 1
62 head, tail = p[:i], p[i:]
63 if head and head <> '/'*len(head):
64 while head[-1] == '/':
65 head = head[:-1]
66 return head, tail
Guido van Rossum5c971671996-07-22 15:23:25 +000067
68
69# Split a path in root and extension.
Guido van Rossuma8763e51996-08-26 18:33:32 +000070# The extension is everything starting at the last dot in the last
Guido van Rossum5c971671996-07-22 15:23:25 +000071# pathname component; the root is everything before that.
72# It is always true that root + ext == p.
73
74def splitext(p):
Guido van Rossumb978d181997-12-09 16:56:41 +000075 """Split the extension from a pathname. Extension is everything from the
76last dot to the end. Returns "(root, ext)", either part may be empty"""
77 root, ext = '', ''
78 for c in p:
79 if c == '/':
80 root, ext = root + ext + c, ''
81 elif c == '.':
82 if ext:
83 root, ext = root + ext, c
84 else:
85 ext = c
86 elif ext:
87 ext = ext + c
88 else:
89 root = root + c
90 return root, ext
Guido van Rossum5c971671996-07-22 15:23:25 +000091
92
93# Split a pathname into a drive specification and the rest of the
94# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
95
96def splitdrive(p):
Guido van Rossumb978d181997-12-09 16:56:41 +000097 """Split a pathname into drive and path. On Posix, drive is always
98empty"""
99 return '', p
Guido van Rossum5c971671996-07-22 15:23:25 +0000100
101
102# Return the tail (basename) part of a path.
103
104def basename(p):
Guido van Rossumb978d181997-12-09 16:56:41 +0000105 """Returns the final component of a pathname"""
106 return split(p)[1]
Guido van Rossum5c971671996-07-22 15:23:25 +0000107
108
109# Return the head (dirname) part of a path.
110
111def dirname(p):
Guido van Rossumb978d181997-12-09 16:56:41 +0000112 """Returns the directory component of a pathname"""
113 return split(p)[0]
Guido van Rossum5c971671996-07-22 15:23:25 +0000114
115
116# Return the longest prefix of all list elements.
117
118def commonprefix(m):
Guido van Rossumb978d181997-12-09 16:56:41 +0000119 "Given a list of pathnames, returns the longest common leading component"
120 if not m: return ''
121 prefix = m[0]
122 for item in m:
123 for i in range(len(prefix)):
124 if prefix[:i+1] <> item[:i+1]:
125 prefix = prefix[:i]
126 if i == 0: return ''
127 break
128 return prefix
Guido van Rossum5c971671996-07-22 15:23:25 +0000129
130
Guido van Rossume03c0501998-08-12 02:38:11 +0000131# Get size, mtime, atime of files.
132
133def getsize(filename):
134 """Return the size of a file, reported by os.stat()."""
135 st = os.stat(filename)
136 return st[stat.ST_SIZE]
137
138def getmtime(filename):
139 """Return the last modification time of a file, reported by os.stat()."""
140 st = os.stat(filename)
141 return st[stat.ST_MTIME]
142
143def getatime(filename):
144 """Return the last access time of a file, reported by os.stat()."""
145 st = os.stat(filename)
Guido van Rossumaad67612000-05-08 17:31:04 +0000146 return st[stat.ST_ATIME]
Guido van Rossume03c0501998-08-12 02:38:11 +0000147
148
Guido van Rossum5c971671996-07-22 15:23:25 +0000149# Is a path a symbolic link?
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000150# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum5c971671996-07-22 15:23:25 +0000151
152def islink(path):
Guido van Rossumb978d181997-12-09 16:56:41 +0000153 """Test whether a path is a symbolic link"""
154 try:
155 st = os.lstat(path)
156 except (os.error, AttributeError):
157 return 0
158 return stat.S_ISLNK(st[stat.ST_MODE])
Guido van Rossum5c971671996-07-22 15:23:25 +0000159
160
161# Does a path exist?
162# This is false for dangling symbolic links.
163
164def exists(path):
Guido van Rossumb978d181997-12-09 16:56:41 +0000165 """Test whether a path exists. Returns false for broken symbolic links"""
166 try:
167 st = os.stat(path)
168 except os.error:
169 return 0
170 return 1
Guido van Rossum5c971671996-07-22 15:23:25 +0000171
172
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000173# Is a path a directory?
Guido van Rossum5c971671996-07-22 15:23:25 +0000174# This follows symbolic links, so both islink() and isdir() can be true
175# for the same path.
176
177def isdir(path):
Guido van Rossumb978d181997-12-09 16:56:41 +0000178 """Test whether a path is a directory"""
179 try:
180 st = os.stat(path)
181 except os.error:
182 return 0
183 return stat.S_ISDIR(st[stat.ST_MODE])
Guido van Rossum5c971671996-07-22 15:23:25 +0000184
185
186# Is a path a regular file?
187# This follows symbolic links, so both islink() and isfile() can be true
188# for the same path.
189
190def isfile(path):
Guido van Rossumb978d181997-12-09 16:56:41 +0000191 """Test whether a path is a regular file"""
192 try:
193 st = os.stat(path)
194 except os.error:
195 return 0
196 return stat.S_ISREG(st[stat.ST_MODE])
Guido van Rossum5c971671996-07-22 15:23:25 +0000197
198
199# Are two filenames really pointing to the same file?
200
201def samefile(f1, f2):
Guido van Rossumb978d181997-12-09 16:56:41 +0000202 """Test whether two pathnames reference the same actual file"""
203 s1 = os.stat(f1)
204 s2 = os.stat(f2)
205 return samestat(s1, s2)
Guido van Rossum5c971671996-07-22 15:23:25 +0000206
207
208# Are two open files really referencing the same file?
209# (Not necessarily the same file descriptor!)
Guido van Rossum5c971671996-07-22 15:23:25 +0000210
211def sameopenfile(fp1, fp2):
Guido van Rossumb978d181997-12-09 16:56:41 +0000212 """Test whether two open file objects reference the same file"""
213 s1 = os.fstat(fp1)
214 s2 = os.fstat(fp2)
215 return samestat(s1, s2)
Guido van Rossum5c971671996-07-22 15:23:25 +0000216
217
218# Are two stat buffers (obtained from stat, fstat or lstat)
219# describing the same file?
220
221def samestat(s1, s2):
Guido van Rossumb978d181997-12-09 16:56:41 +0000222 """Test whether two stat buffers reference the same file"""
223 return s1[stat.ST_INO] == s2[stat.ST_INO] and \
224 s1[stat.ST_DEV] == s2[stat.ST_DEV]
Guido van Rossum5c971671996-07-22 15:23:25 +0000225
226
227# Is a path a mount point?
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000228# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
Guido van Rossum5c971671996-07-22 15:23:25 +0000229
230def ismount(path):
Guido van Rossumb978d181997-12-09 16:56:41 +0000231 """Test whether a path is a mount point"""
232 try:
233 s1 = os.stat(path)
234 s2 = os.stat(join(path, '..'))
235 except os.error:
236 return 0 # It doesn't exist -- so not a mount point :-)
237 dev1 = s1[stat.ST_DEV]
238 dev2 = s2[stat.ST_DEV]
239 if dev1 != dev2:
240 return 1 # path/.. on a different device as path
241 ino1 = s1[stat.ST_INO]
242 ino2 = s2[stat.ST_INO]
243 if ino1 == ino2:
244 return 1 # path/.. is the same i-node as path
245 return 0
Guido van Rossum5c971671996-07-22 15:23:25 +0000246
247
248# Directory tree walk.
249# For each directory under top (including top itself, but excluding
250# '.' and '..'), func(arg, dirname, filenames) is called, where
251# dirname is the name of the directory and filenames is the list
Guido van Rossumb978d181997-12-09 16:56:41 +0000252# of files (and subdirectories etc.) in the directory.
Guido van Rossum5c971671996-07-22 15:23:25 +0000253# The func may modify the filenames list, to implement a filter,
254# or to impose a different order of visiting.
255
256def walk(top, func, arg):
Guido van Rossumaad67612000-05-08 17:31:04 +0000257 """walk(top,func,arg) calls func(arg, d, files) for each directory "d"
Guido van Rossumb978d181997-12-09 16:56:41 +0000258in the tree rooted at "top" (including "top" itself). "files" is a list
259of all the files and subdirs in directory "d".
260"""
261 try:
262 names = os.listdir(top)
263 except os.error:
264 return
265 func(arg, top, names)
Guido van Rossumb978d181997-12-09 16:56:41 +0000266 for name in names:
Guido van Rossumb978d181997-12-09 16:56:41 +0000267 name = join(top, name)
Guido van Rossumaad67612000-05-08 17:31:04 +0000268 st = os.lstat(name)
269 if stat.S_ISDIR(st[stat.ST_MODE]):
Guido van Rossumb978d181997-12-09 16:56:41 +0000270 walk(name, func, arg)
Guido van Rossum5c971671996-07-22 15:23:25 +0000271
272
273# Expand paths beginning with '~' or '~user'.
274# '~' means $HOME; '~user' means that user's home directory.
275# If the path doesn't begin with '~', or if the user or $HOME is unknown,
276# the path is returned unchanged (leaving error reporting to whatever
277# function is called with the expanded path as argument).
278# See also module 'glob' for expansion of *, ? and [...] in pathnames.
279# (A function should also be defined to do full *sh-style environment
280# variable expansion.)
281
282def expanduser(path):
Guido van Rossumb978d181997-12-09 16:56:41 +0000283 """Expand ~ and ~user constructions. If user or $HOME is unknown,
284do nothing"""
285 if path[:1] <> '~':
286 return path
287 i, n = 1, len(path)
288 while i < n and path[i] <> '/':
289 i = i+1
290 if i == 1:
291 if not os.environ.has_key('HOME'):
292 return path
293 userhome = os.environ['HOME']
294 else:
295 import pwd
296 try:
297 pwent = pwd.getpwnam(path[1:i])
298 except KeyError:
299 return path
300 userhome = pwent[5]
301 if userhome[-1:] == '/': i = i+1
302 return userhome + path[i:]
Guido van Rossum5c971671996-07-22 15:23:25 +0000303
304
305# Expand paths containing shell variable substitutions.
306# This expands the forms $variable and ${variable} only.
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000307# Non-existent variables are left unchanged.
Guido van Rossum5c971671996-07-22 15:23:25 +0000308
309_varprog = None
310
311def expandvars(path):
Guido van Rossumb978d181997-12-09 16:56:41 +0000312 """Expand shell variables of form $var and ${var}. Unknown variables
313are left unchanged"""
314 global _varprog
315 if '$' not in path:
316 return path
317 if not _varprog:
318 import re
319 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
320 i = 0
321 while 1:
322 m = _varprog.search(path, i)
323 if not m:
324 break
325 i, j = m.span(0)
326 name = m.group(1)
327 if name[:1] == '{' and name[-1:] == '}':
328 name = name[1:-1]
329 if os.environ.has_key(name):
330 tail = path[j:]
331 path = path[:i] + os.environ[name]
332 i = len(path)
333 path = path + tail
334 else:
335 i = j
336 return path
Guido van Rossum5c971671996-07-22 15:23:25 +0000337
338
339# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
340# It should be understood that this may change the meaning of the path
341# if it contains symbolic links!
342
343def normpath(path):
Guido van Rossumb978d181997-12-09 16:56:41 +0000344 """Normalize path, eliminating double slashes, etc."""
345 import string
346 # Treat initial slashes specially
347 slashes = ''
348 while path[:1] == '/':
349 slashes = slashes + '/'
350 path = path[1:]
351 comps = string.splitfields(path, '/')
352 i = 0
353 while i < len(comps):
354 if comps[i] == '.':
355 del comps[i]
Guido van Rossum64e736b1998-10-02 01:23:47 +0000356 while i < len(comps) and comps[i] == '':
357 del comps[i]
Guido van Rossumb978d181997-12-09 16:56:41 +0000358 elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
359 del comps[i-1:i+1]
360 i = i-1
361 elif comps[i] == '' and i > 0 and comps[i-1] <> '':
362 del comps[i]
363 else:
364 i = i+1
365 # If the path is now empty, substitute '.'
366 if not comps and not slashes:
367 comps.append('.')
368 return slashes + string.joinfields(comps, '/')
Guido van Rossum2e7840f1999-02-09 18:40:13 +0000369
370
Guido van Rossum2e7840f1999-02-09 18:40:13 +0000371def abspath(path):
Guido van Rossumaad67612000-05-08 17:31:04 +0000372 """Return an absolute path."""
Guido van Rossum2e7840f1999-02-09 18:40:13 +0000373 if not isabs(path):
374 path = join(os.getcwd(), path)
375 return normpath(path)