blob: 965184bc3c9a464b283e2182d742e2e5c623facb [file] [log] [blame]
Guido van Rossum3bb710d1996-07-30 16:35:26 +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 Rossum5c971671996-07-22 15:23:25 +00008
Guido van Rossum3bb710d1996-07-30 16:35:26 +00009import os
Guido van Rossum5c971671996-07-22 15:23:25 +000010import stat
11
12
13# 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
Guido van Rossum228b8e81997-04-02 06:13:34 +000029# Join pathnames.
30# Ignore the previous parts if a part is absolute.
Guido van Rossum5c971671996-07-22 15:23:25 +000031# Insert a '/' unless the first part is empty or already ends in '/'.
32
Guido van Rossum228b8e81997-04-02 06:13:34 +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 Rossum5c971671996-07-22 15:23:25 +000043
44
45# Split a path in head (everything up to the last '/') and tail (the
46# 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.
49
50def split(p):
51 import string
52 i = string.rfind(p, '/') + 1
53 head, tail = p[:i], p[i:]
54 if head and head <> '/'*len(head):
55 while head[-1] == '/':
56 head = head[:-1]
57 return head, tail
58
59
60# Split a path in root and extension.
Guido van Rossuma8763e51996-08-26 18:33:32 +000061# The extension is everything starting at the last dot in the last
Guido van Rossum5c971671996-07-22 15:23:25 +000062# pathname component; the root is everything before that.
63# It is always true that root + ext == p.
64
65def splitext(p):
66 root, ext = '', ''
67 for c in p:
68 if c == '/':
69 root, ext = root + ext + c, ''
70 elif c == '.':
71 if ext:
72 root, ext = root + ext, c
73 else:
74 ext = c
75 elif ext:
76 ext = ext + c
77 else:
78 root = root + c
79 return root, ext
80
81
82# 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
89# Return the tail (basename) part of a path.
90
91def basename(p):
92 return split(p)[1]
93
94
95# Return the head (dirname) part of a path.
96
97def dirname(p):
98 return split(p)[0]
99
100
101# Return the longest prefix of all list elements.
102
103def 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]
110 if i == 0: return ''
111 break
112 return prefix
113
114
115# Is a path a symbolic link?
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000116# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum5c971671996-07-22 15:23:25 +0000117
118def islink(path):
119 try:
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000120 st = os.lstat(path)
121 except (os.error, AttributeError):
Guido van Rossum5c971671996-07-22 15:23:25 +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
129def exists(path):
130 try:
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000131 st = os.stat(path)
132 except os.error:
Guido van Rossum5c971671996-07-22 15:23:25 +0000133 return 0
134 return 1
135
136
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000137# Is a path a directory?
Guido van Rossum5c971671996-07-22 15:23:25 +0000138# This follows symbolic links, so both islink() and isdir() can be true
139# for the same path.
140
141def isdir(path):
142 try:
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000143 st = os.stat(path)
144 except os.error:
Guido van Rossum5c971671996-07-22 15:23:25 +0000145 return 0
146 return stat.S_ISDIR(st[stat.ST_MODE])
147
148
149# Is a path a regular file?
150# This follows symbolic links, so both islink() and isfile() can be true
151# for the same path.
152
153def isfile(path):
154 try:
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000155 st = os.stat(path)
156 except os.error:
Guido van Rossum5c971671996-07-22 15:23:25 +0000157 return 0
158 return stat.S_ISREG(st[stat.ST_MODE])
159
160
161# Are two filenames really pointing to the same file?
162
163def samefile(f1, f2):
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000164 s1 = os.stat(f1)
165 s2 = os.stat(f2)
Guido van Rossum5c971671996-07-22 15:23:25 +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 Rossum5c971671996-07-22 15:23:25 +0000171
172def sameopenfile(fp1, fp2):
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000173 s1 = os.fstat(fp1)
174 s2 = os.fstat(fp2)
Guido van Rossum5c971671996-07-22 15:23:25 +0000175 return samestat(s1, s2)
176
177
178# Are two stat buffers (obtained from stat, fstat or lstat)
179# describing the same file?
180
181def samestat(s1, s2):
182 return s1[stat.ST_INO] == s2[stat.ST_INO] and \
183 s1[stat.ST_DEV] == s2[stat.ST_DEV]
184
185
186# Is a path a mount point?
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000187# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
Guido van Rossum5c971671996-07-22 15:23:25 +0000188
189def ismount(path):
190 try:
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000191 s1 = os.stat(path)
192 s2 = os.stat(join(path, '..'))
193 except os.error:
Guido van Rossum5c971671996-07-22 15:23:25 +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
204
205
206# Directory tree walk.
207# 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,
212# or to impose a different order of visiting.
213
214def walk(top, func, arg):
215 try:
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000216 names = os.listdir(top)
217 except os.error:
Guido van Rossum5c971671996-07-22 15:23:25 +0000218 return
219 func(arg, top, names)
220 exceptions = ('.', '..')
221 for name in names:
222 if name not in exceptions:
223 name = join(top, name)
224 if isdir(name) and not islink(name):
225 walk(name, func, arg)
226
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 Rossum3bb710d1996-07-30 16:35:26 +0000244 if not os.environ.has_key('HOME'):
Guido van Rossum5c971671996-07-22 15:23:25 +0000245 return path
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000246 userhome = os.environ['HOME']
Guido van Rossum5c971671996-07-22 15:23:25 +0000247 else:
248 import pwd
249 try:
250 pwent = pwd.getpwnam(path[1:i])
251 except KeyError:
252 return path
253 userhome = pwent[5]
254 if userhome[-1:] == '/': i = i+1
255 return userhome + path[i:]
256
257
258# Expand paths containing shell variable substitutions.
259# This expands the forms $variable and ${variable} only.
260# Non-existant variables are left unchanged.
261
262_varprog = None
263
264def expandvars(path):
265 global _varprog
266 if '$' not in path:
267 return path
268 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 Rossum3bb710d1996-07-30 16:35:26 +0000280 if os.environ.has_key(name):
Guido van Rossum5c971671996-07-22 15:23:25 +0000281 tail = path[j:]
Guido van Rossum3bb710d1996-07-30 16:35:26 +0000282 path = path[:i] + os.environ[name]
Guido van Rossum5c971671996-07-22 15:23:25 +0000283 i = len(path)
284 path = path + tail
285 else:
286 i = j
287 return path
288
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
296 # Treat initial slashes specially
297 slashes = ''
298 while path[:1] == '/':
299 slashes = slashes + '/'
300 path = path[1:]
301 comps = string.splitfields(path, '/')
302 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 '.'
315 if not comps and not slashes:
316 comps.append('.')
317 return slashes + string.joinfields(comps, '/')