blob: 965184bc3c9a464b283e2182d742e2e5c623facb [file] [log] [blame]
Guido van Rossumd3876d31996-07-23 03:47:28 +00001# Module 'posixpath' -- common operations on Posix pathnames.
2# Some of this can actually be useful on non-Posix systems too, e.g.
3# for manipulation of the pathname component of URLs.
4# The "os.path" name is an alias for this module on Posix systems;
5# on other systems (e.g. Mac, Windows), os.path provides the same
6# operations in a manner specific to that platform, and is an alias
7# to another module (e.g. macpath, ntpath).
Guido van Rossumc6360141990-10-13 19:23:40 +00008
Guido van Rossumd3876d31996-07-23 03:47:28 +00009import os
Guido van Rossum40d93041990-10-21 16:17:34 +000010import stat
Guido van Rossumc6360141990-10-13 19:23:40 +000011
12
Guido van Rossum7ac48781992-01-14 18:29:32 +000013# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
14# On MS-DOS this may also turn slashes into backslashes; however, other
15# normalizations (such as optimizing '../' away) are not allowed
16# (another function should be defined to do that).
17
18def normcase(s):
19 return s
20
21
22# Return wheter a path is absolute.
23# Trivial in Posix, harder on the Mac or MS-DOS.
24
25def isabs(s):
26 return s[:1] == '/'
27
28
Barry Warsaw384d2491997-02-18 21:53:25 +000029# Join pathnames.
30# Ignore the previous parts if a part is absolute.
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000031# Insert a '/' unless the first part is empty or already ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000032
Barry Warsaw384d2491997-02-18 21:53:25 +000033def join(a, *p):
34 path = a
35 for b in p:
36 if b[:1] == '/':
37 path = b
38 elif path == '' or path[-1:] == '/':
39 path = path + b
40 else:
41 path = path + '/' + b
42 return path
Guido van Rossumc6360141990-10-13 19:23:40 +000043
44
Guido van Rossum26847381992-03-31 18:54:35 +000045# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000046# rest). If the path ends in '/', tail will be empty. If there is no
47# '/' in the path, head will be empty.
48# Trailing '/'es are stripped from head unless it is the root.
Guido van Rossum7ac48781992-01-14 18:29:32 +000049
Guido van Rossumc6360141990-10-13 19:23:40 +000050def split(p):
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000051 import string
52 i = string.rfind(p, '/') + 1
53 head, tail = p[:i], p[i:]
54 if head and head <> '/'*len(head):
Guido van Rossum26847381992-03-31 18:54:35 +000055 while head[-1] == '/':
56 head = head[:-1]
Guido van Rossumc6360141990-10-13 19:23:40 +000057 return head, tail
58
59
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000060# Split a path in root and extension.
Guido van Rossum422869a1996-08-20 20:24:17 +000061# The extension is everything starting at the last dot in the last
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000062# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +000063# It is always true that root + ext == p.
64
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000065def splitext(p):
66 root, ext = '', ''
67 for c in p:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000068 if c == '/':
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000069 root, ext = root + ext + c, ''
Sjoerd Mullender43598601994-12-14 15:29:17 +000070 elif c == '.':
71 if ext:
72 root, ext = root + ext, c
73 else:
74 ext = c
75 elif ext:
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000076 ext = ext + c
77 else:
78 root = root + c
79 return root, ext
80
81
Guido van Rossum221df241995-08-07 20:17:55 +000082# Split a pathname into a drive specification and the rest of the
83# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
84
85def splitdrive(p):
86 return '', p
87
88
Guido van Rossumc6360141990-10-13 19:23:40 +000089# Return the tail (basename) part of a path.
Guido van Rossum7ac48781992-01-14 18:29:32 +000090
Guido van Rossumc6360141990-10-13 19:23:40 +000091def basename(p):
92 return split(p)[1]
93
94
Guido van Rossumc629d341992-11-05 10:43:02 +000095# Return the head (dirname) part of a path.
96
97def dirname(p):
98 return split(p)[0]
99
100
Guido van Rossumc6360141990-10-13 19:23:40 +0000101# Return the longest prefix of all list elements.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000102
Guido van Rossumc6360141990-10-13 19:23:40 +0000103def commonprefix(m):
104 if not m: return ''
105 prefix = m[0]
106 for item in m:
107 for i in range(len(prefix)):
108 if prefix[:i+1] <> item[:i+1]:
109 prefix = prefix[:i]
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000110 if i == 0: return ''
Guido van Rossumc6360141990-10-13 19:23:40 +0000111 break
112 return prefix
113
114
Guido van Rossum7ac48781992-01-14 18:29:32 +0000115# Is a path a symbolic link?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000116# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000117
118def islink(path):
119 try:
Guido van Rossumd3876d31996-07-23 03:47:28 +0000120 st = os.lstat(path)
121 except (os.error, AttributeError):
Guido van Rossum7ac48781992-01-14 18:29:32 +0000122 return 0
123 return stat.S_ISLNK(st[stat.ST_MODE])
124
125
126# Does a path exist?
127# This is false for dangling symbolic links.
128
Guido van Rossumc6360141990-10-13 19:23:40 +0000129def exists(path):
130 try:
Guido van Rossumd3876d31996-07-23 03:47:28 +0000131 st = os.stat(path)
132 except os.error:
Guido van Rossumc6360141990-10-13 19:23:40 +0000133 return 0
134 return 1
135
136
Guido van Rossumd3876d31996-07-23 03:47:28 +0000137# Is a path a directory?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000138# This follows symbolic links, so both islink() and isdir() can be true
139# for the same path.
140
Guido van Rossumc6360141990-10-13 19:23:40 +0000141def isdir(path):
142 try:
Guido van Rossumd3876d31996-07-23 03:47:28 +0000143 st = os.stat(path)
144 except os.error:
Guido van Rossumc6360141990-10-13 19:23:40 +0000145 return 0
Guido van Rossum40d93041990-10-21 16:17:34 +0000146 return stat.S_ISDIR(st[stat.ST_MODE])
Guido van Rossumc6360141990-10-13 19:23:40 +0000147
148
Guido van Rossum26847381992-03-31 18:54:35 +0000149# Is a path a regular file?
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150# This follows symbolic links, so both islink() and isfile() can be true
Guido van Rossum7ac48781992-01-14 18:29:32 +0000151# for the same path.
152
153def isfile(path):
Guido van Rossumc6360141990-10-13 19:23:40 +0000154 try:
Guido van Rossumd3876d31996-07-23 03:47:28 +0000155 st = os.stat(path)
156 except os.error:
Guido van Rossumc6360141990-10-13 19:23:40 +0000157 return 0
Guido van Rossum7ac48781992-01-14 18:29:32 +0000158 return stat.S_ISREG(st[stat.ST_MODE])
Guido van Rossumc6360141990-10-13 19:23:40 +0000159
160
Guido van Rossumd3778f91991-11-12 15:37:40 +0000161# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000162
Guido van Rossumd3778f91991-11-12 15:37:40 +0000163def samefile(f1, f2):
Guido van Rossumd3876d31996-07-23 03:47:28 +0000164 s1 = os.stat(f1)
165 s2 = os.stat(f2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000166 return samestat(s1, s2)
167
168
169# Are two open files really referencing the same file?
170# (Not necessarily the same file descriptor!)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000171
Guido van Rossumd3778f91991-11-12 15:37:40 +0000172def sameopenfile(fp1, fp2):
Guido van Rossumd3876d31996-07-23 03:47:28 +0000173 s1 = os.fstat(fp1)
174 s2 = os.fstat(fp2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000175 return samestat(s1, s2)
176
177
178# Are two stat buffers (obtained from stat, fstat or lstat)
179# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000180
Guido van Rossumd3778f91991-11-12 15:37:40 +0000181def samestat(s1, s2):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000182 return s1[stat.ST_INO] == s2[stat.ST_INO] and \
Guido van Rossum509d24a1992-05-06 11:36:49 +0000183 s1[stat.ST_DEV] == s2[stat.ST_DEV]
Guido van Rossumc6360141990-10-13 19:23:40 +0000184
185
186# Is a path a mount point?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000187# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000188
Guido van Rossumc6360141990-10-13 19:23:40 +0000189def ismount(path):
Guido van Rossum509d24a1992-05-06 11:36:49 +0000190 try:
Guido van Rossumd3876d31996-07-23 03:47:28 +0000191 s1 = os.stat(path)
192 s2 = os.stat(join(path, '..'))
193 except os.error:
Guido van Rossum509d24a1992-05-06 11:36:49 +0000194 return 0 # It doesn't exist -- so not a mount point :-)
195 dev1 = s1[stat.ST_DEV]
196 dev2 = s2[stat.ST_DEV]
197 if dev1 != dev2:
198 return 1 # path/.. on a different device as path
199 ino1 = s1[stat.ST_INO]
200 ino2 = s2[stat.ST_INO]
201 if ino1 == ino2:
202 return 1 # path/.. is the same i-node as path
203 return 0
Guido van Rossumc6360141990-10-13 19:23:40 +0000204
205
206# Directory tree walk.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000207# For each directory under top (including top itself, but excluding
208# '.' and '..'), func(arg, dirname, filenames) is called, where
209# dirname is the name of the directory and filenames is the list
210# files files (and subdirectories etc.) in the directory.
211# The func may modify the filenames list, to implement a filter,
Guido van Rossumc6360141990-10-13 19:23:40 +0000212# or to impose a different order of visiting.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000213
Guido van Rossumc6360141990-10-13 19:23:40 +0000214def walk(top, func, arg):
215 try:
Guido van Rossumd3876d31996-07-23 03:47:28 +0000216 names = os.listdir(top)
217 except os.error:
Guido van Rossumc6360141990-10-13 19:23:40 +0000218 return
219 func(arg, top, names)
220 exceptions = ('.', '..')
221 for name in names:
222 if name not in exceptions:
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000223 name = join(top, name)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224 if isdir(name) and not islink(name):
Guido van Rossumc6360141990-10-13 19:23:40 +0000225 walk(name, func, arg)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000226
227
228# Expand paths beginning with '~' or '~user'.
229# '~' means $HOME; '~user' means that user's home directory.
230# If the path doesn't begin with '~', or if the user or $HOME is unknown,
231# the path is returned unchanged (leaving error reporting to whatever
232# function is called with the expanded path as argument).
233# See also module 'glob' for expansion of *, ? and [...] in pathnames.
234# (A function should also be defined to do full *sh-style environment
235# variable expansion.)
236
237def expanduser(path):
238 if path[:1] <> '~':
239 return path
240 i, n = 1, len(path)
241 while i < n and path[i] <> '/':
242 i = i+1
243 if i == 1:
Guido van Rossumd3876d31996-07-23 03:47:28 +0000244 if not os.environ.has_key('HOME'):
Guido van Rossum7ac48781992-01-14 18:29:32 +0000245 return path
Guido van Rossumd3876d31996-07-23 03:47:28 +0000246 userhome = os.environ['HOME']
Guido van Rossum7ac48781992-01-14 18:29:32 +0000247 else:
248 import pwd
249 try:
250 pwent = pwd.getpwnam(path[1:i])
251 except KeyError:
252 return path
253 userhome = pwent[5]
Guido van Rossumbbb4e101996-04-02 22:30:03 +0000254 if userhome[-1:] == '/': i = i+1
Guido van Rossum7ac48781992-01-14 18:29:32 +0000255 return userhome + path[i:]
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000256
257
258# Expand paths containing shell variable substitutions.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259# This expands the forms $variable and ${variable} only.
260# Non-existant variables are left unchanged.
261
262_varprog = None
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000263
264def expandvars(path):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265 global _varprog
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000266 if '$' not in path:
267 return path
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268 if not _varprog:
269 import regex
270 _varprog = regex.compile('$\([a-zA-Z0-9_]+\|{[^}]*}\)')
271 i = 0
272 while 1:
273 i = _varprog.search(path, i)
274 if i < 0:
275 break
276 name = _varprog.group(1)
277 j = i + len(_varprog.group(0))
278 if name[:1] == '{' and name[-1:] == '}':
279 name = name[1:-1]
Guido van Rossumd3876d31996-07-23 03:47:28 +0000280 if os.environ.has_key(name):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281 tail = path[j:]
Guido van Rossumd3876d31996-07-23 03:47:28 +0000282 path = path[:i] + os.environ[name]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283 i = len(path)
284 path = path + tail
285 else:
286 i = j
287 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000288
289
290# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
291# It should be understood that this may change the meaning of the path
292# if it contains symbolic links!
293
294def normpath(path):
295 import string
Guido van Rossumdf563861993-07-06 15:19:36 +0000296 # Treat initial slashes specially
297 slashes = ''
298 while path[:1] == '/':
299 slashes = slashes + '/'
300 path = path[1:]
Guido van Rossumc629d341992-11-05 10:43:02 +0000301 comps = string.splitfields(path, '/')
Guido van Rossumc629d341992-11-05 10:43:02 +0000302 i = 0
303 while i < len(comps):
304 if comps[i] == '.':
305 del comps[i]
306 elif comps[i] == '..' and i > 0 and \
307 comps[i-1] not in ('', '..'):
308 del comps[i-1:i+1]
309 i = i-1
310 elif comps[i] == '' and i > 0 and comps[i-1] <> '':
311 del comps[i]
312 else:
313 i = i+1
314 # If the path is now empty, substitute '.'
Guido van Rossumdf563861993-07-06 15:19:36 +0000315 if not comps and not slashes:
Guido van Rossumc629d341992-11-05 10:43:02 +0000316 comps.append('.')
Guido van Rossumdf563861993-07-06 15:19:36 +0000317 return slashes + string.joinfields(comps, '/')