blob: 7ee4911d3146bd069f80b7de19dc21f9032442ff [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
Skip Montanaroc62c81e2001-02-12 02:00:42 +000016__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
17 "basename","dirname","commonprefix","getsize","getmtime",
Martin v. Löwis96a60e42002-12-31 13:11:54 +000018 "getatime","getctime","islink","exists","isdir","isfile","ismount",
Skip Montanaroc62c81e2001-02-12 02:00:42 +000019 "walk","expanduser","expandvars","normpath","abspath",
Neal Norwitz61cdac62003-01-03 18:01:57 +000020 "samefile","sameopenfile","samestat",
Skip Montanaro117910d2003-02-14 19:35:31 +000021 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
Neal Norwitz61cdac62003-01-03 18:01:57 +000022 "realpath","supports_unicode_filenames"]
Guido van Rossumc6360141990-10-13 19:23:40 +000023
Skip Montanaro117910d2003-02-14 19:35:31 +000024# strings representing various path-related bits and pieces
25curdir = '.'
26pardir = '..'
27extsep = '.'
28sep = '/'
29pathsep = ':'
30defpath = ':/bin:/usr/bin'
31altsep = None
32
Guido van Rossum7ac48781992-01-14 18:29:32 +000033# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
34# On MS-DOS this may also turn slashes into backslashes; however, other
35# normalizations (such as optimizing '../' away) are not allowed
36# (another function should be defined to do that).
37
38def normcase(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000039 """Normalize case of pathname. Has no effect under Posix"""
40 return s
Guido van Rossum7ac48781992-01-14 18:29:32 +000041
42
Jeremy Hyltona05e2932000-06-28 14:48:01 +000043# Return whether a path is absolute.
Guido van Rossum7ac48781992-01-14 18:29:32 +000044# Trivial in Posix, harder on the Mac or MS-DOS.
45
46def isabs(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000047 """Test whether a path is absolute"""
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000048 return s.startswith('/')
Guido van Rossum7ac48781992-01-14 18:29:32 +000049
50
Barry Warsaw384d2491997-02-18 21:53:25 +000051# Join pathnames.
52# Ignore the previous parts if a part is absolute.
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000053# Insert a '/' unless the first part is empty or already ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000054
Barry Warsaw384d2491997-02-18 21:53:25 +000055def join(a, *p):
Guido van Rossum346f7af1997-12-05 19:04:51 +000056 """Join two or more pathname components, inserting '/' as needed"""
57 path = a
58 for b in p:
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000059 if b.startswith('/'):
Guido van Rossum346f7af1997-12-05 19:04:51 +000060 path = b
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000061 elif path == '' or path.endswith('/'):
62 path += b
Guido van Rossum346f7af1997-12-05 19:04:51 +000063 else:
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000064 path += '/' + b
Guido van Rossum346f7af1997-12-05 19:04:51 +000065 return path
Guido van Rossumc6360141990-10-13 19:23:40 +000066
67
Guido van Rossum26847381992-03-31 18:54:35 +000068# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000069# rest). If the path ends in '/', tail will be empty. If there is no
70# '/' in the path, head will be empty.
71# Trailing '/'es are stripped from head unless it is the root.
Guido van Rossum7ac48781992-01-14 18:29:32 +000072
Guido van Rossumc6360141990-10-13 19:23:40 +000073def split(p):
Tim Peters2344fae2001-01-15 00:50:52 +000074 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
Fred Drakec0ab93e2000-09-28 16:22:52 +000075 everything after the final slash. Either part may be empty."""
Fred Drake22fb8392000-09-28 15:04:39 +000076 i = p.rfind('/') + 1
Guido van Rossum346f7af1997-12-05 19:04:51 +000077 head, tail = p[:i], p[i:]
Fred Drake8152d322000-12-12 23:20:45 +000078 if head and head != '/'*len(head):
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000079 head = head.rstrip('/')
Guido van Rossum346f7af1997-12-05 19:04:51 +000080 return head, tail
Guido van Rossumc6360141990-10-13 19:23:40 +000081
82
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000083# Split a path in root and extension.
Guido van Rossum422869a1996-08-20 20:24:17 +000084# The extension is everything starting at the last dot in the last
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000085# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +000086# It is always true that root + ext == p.
87
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000088def splitext(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +000089 """Split the extension from a pathname. Extension is everything from the
Fred Drakec0ab93e2000-09-28 16:22:52 +000090 last dot to the end. Returns "(root, ext)", either part may be empty."""
Martin v. Löwisde333792002-12-12 20:30:20 +000091 i = p.rfind('.')
92 if i<=p.rfind('/'):
93 return p, ''
94 else:
95 return p[:i], p[i:]
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000096
97
Guido van Rossum221df241995-08-07 20:17:55 +000098# Split a pathname into a drive specification and the rest of the
99# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
100
101def splitdrive(p):
Tim Peters2344fae2001-01-15 00:50:52 +0000102 """Split a pathname into drive and path. On Posix, drive is always
Fred Drakec0ab93e2000-09-28 16:22:52 +0000103 empty."""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000104 return '', p
Guido van Rossum221df241995-08-07 20:17:55 +0000105
106
Guido van Rossumc6360141990-10-13 19:23:40 +0000107# Return the tail (basename) part of a path.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000108
Guido van Rossumc6360141990-10-13 19:23:40 +0000109def basename(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000110 """Returns the final component of a pathname"""
111 return split(p)[1]
Guido van Rossumc6360141990-10-13 19:23:40 +0000112
113
Guido van Rossumc629d341992-11-05 10:43:02 +0000114# Return the head (dirname) part of a path.
115
116def dirname(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000117 """Returns the directory component of a pathname"""
118 return split(p)[0]
Guido van Rossumc629d341992-11-05 10:43:02 +0000119
120
Guido van Rossumc6360141990-10-13 19:23:40 +0000121# Return the longest prefix of all list elements.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000122
Guido van Rossumc6360141990-10-13 19:23:40 +0000123def commonprefix(m):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000124 "Given a list of pathnames, returns the longest common leading component"
125 if not m: return ''
Raymond Hettinger74bb7f02003-12-31 22:44:29 +0000126 s1 = min(m)
127 s2 = max(m)
128 n = min(len(s1), len(s2))
129 for i in xrange(n):
130 if s1[i] != s2[i]:
131 return s1[:i]
132 return s1[:n]
Guido van Rossumc6360141990-10-13 19:23:40 +0000133
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000134# Get size, mtime, atime of files.
135
136def getsize(filename):
137 """Return the size of a file, reported by os.stat()."""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000138 return os.stat(filename).st_size
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000139
140def getmtime(filename):
141 """Return the last modification time of a file, reported by os.stat()."""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000142 return os.stat(filename).st_mtime
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000143
144def getatime(filename):
145 """Return the last access time of a file, reported by os.stat()."""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000146 return os.stat(filename).st_atime
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000147
Martin v. Löwis96a60e42002-12-31 13:11:54 +0000148def getctime(filename):
149 """Return the creation time of a file, reported by os.stat()."""
150 return os.stat(filename).st_ctime
Guido van Rossum2bc1f8f1998-07-24 20:49:26 +0000151
Guido van Rossum7ac48781992-01-14 18:29:32 +0000152# Is a path a symbolic link?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000153# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000154
155def islink(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000156 """Test whether a path is a symbolic link"""
157 try:
158 st = os.lstat(path)
159 except (os.error, AttributeError):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000160 return False
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000161 return stat.S_ISLNK(st.st_mode)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000162
163
164# Does a path exist?
165# This is false for dangling symbolic links.
166
Guido van Rossumc6360141990-10-13 19:23:40 +0000167def exists(path):
Tim Petersbc0e9102002-04-04 22:55:58 +0000168 """Test whether a path exists. Returns False for broken symbolic links"""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000169 try:
170 st = os.stat(path)
171 except os.error:
Tim Petersbc0e9102002-04-04 22:55:58 +0000172 return False
173 return True
Guido van Rossumc6360141990-10-13 19:23:40 +0000174
175
Guido van Rossumd3876d31996-07-23 03:47:28 +0000176# Is a path a directory?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000177# This follows symbolic links, so both islink() and isdir() can be true
178# for the same path.
179
Guido van Rossumc6360141990-10-13 19:23:40 +0000180def isdir(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000181 """Test whether a path is a directory"""
182 try:
183 st = os.stat(path)
184 except os.error:
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000185 return False
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000186 return stat.S_ISDIR(st.st_mode)
Guido van Rossumc6360141990-10-13 19:23:40 +0000187
188
Guido van Rossum26847381992-03-31 18:54:35 +0000189# Is a path a regular file?
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190# This follows symbolic links, so both islink() and isfile() can be true
Guido van Rossum7ac48781992-01-14 18:29:32 +0000191# for the same path.
192
193def isfile(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000194 """Test whether a path is a regular file"""
195 try:
196 st = os.stat(path)
197 except os.error:
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000198 return False
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000199 return stat.S_ISREG(st.st_mode)
Guido van Rossumc6360141990-10-13 19:23:40 +0000200
201
Guido van Rossumd3778f91991-11-12 15:37:40 +0000202# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000203
Guido van Rossumd3778f91991-11-12 15:37:40 +0000204def samefile(f1, f2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000205 """Test whether two pathnames reference the same actual file"""
206 s1 = os.stat(f1)
207 s2 = os.stat(f2)
208 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000209
210
211# Are two open files really referencing the same file?
212# (Not necessarily the same file descriptor!)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000213
Guido van Rossumd3778f91991-11-12 15:37:40 +0000214def sameopenfile(fp1, fp2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000215 """Test whether two open file objects reference the same file"""
216 s1 = os.fstat(fp1)
217 s2 = os.fstat(fp2)
218 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000219
220
221# Are two stat buffers (obtained from stat, fstat or lstat)
222# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000223
Guido van Rossumd3778f91991-11-12 15:37:40 +0000224def samestat(s1, s2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000225 """Test whether two stat buffers reference the same file"""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000226 return s1.st_ino == s2.st_ino and \
227 s1.st_dev == s2.st_dev
Guido van Rossumc6360141990-10-13 19:23:40 +0000228
229
230# Is a path a mount point?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000231# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000232
Guido van Rossumc6360141990-10-13 19:23:40 +0000233def ismount(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000234 """Test whether a path is a mount point"""
235 try:
236 s1 = os.stat(path)
237 s2 = os.stat(join(path, '..'))
238 except os.error:
Tim Petersbc0e9102002-04-04 22:55:58 +0000239 return False # It doesn't exist -- so not a mount point :-)
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000240 dev1 = s1.st_dev
241 dev2 = s2.st_dev
Guido van Rossum346f7af1997-12-05 19:04:51 +0000242 if dev1 != dev2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000243 return True # path/.. on a different device as path
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000244 ino1 = s1.st_ino
245 ino2 = s2.st_ino
Guido van Rossum346f7af1997-12-05 19:04:51 +0000246 if ino1 == ino2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000247 return True # path/.. is the same i-node as path
248 return False
Guido van Rossumc6360141990-10-13 19:23:40 +0000249
250
251# Directory tree walk.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000252# For each directory under top (including top itself, but excluding
253# '.' and '..'), func(arg, dirname, filenames) is called, where
254# dirname is the name of the directory and filenames is the list
Guido van Rossum346f7af1997-12-05 19:04:51 +0000255# of files (and subdirectories etc.) in the directory.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000256# The func may modify the filenames list, to implement a filter,
Guido van Rossumc6360141990-10-13 19:23:40 +0000257# or to impose a different order of visiting.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000258
Guido van Rossumc6360141990-10-13 19:23:40 +0000259def walk(top, func, arg):
Tim Peterscf5e6a42001-10-10 04:16:20 +0000260 """Directory tree walk with callback function.
261
262 For each directory in the directory tree rooted at top (including top
263 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
264 dirname is the name of the directory, and fnames a list of the names of
265 the files and subdirectories in dirname (excluding '.' and '..'). func
266 may modify the fnames list in-place (e.g. via del or slice assignment),
267 and walk will only recurse into the subdirectories whose names remain in
268 fnames; this can be used to implement a filter, or to impose a specific
269 order of visiting. No semantics are defined for, or required of, arg,
270 beyond that arg is always passed to func. It can be used, e.g., to pass
271 a filename pattern, or a mutable object designed to accumulate
272 statistics. Passing None for arg is common."""
273
Guido van Rossum346f7af1997-12-05 19:04:51 +0000274 try:
275 names = os.listdir(top)
276 except os.error:
277 return
278 func(arg, top, names)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000279 for name in names:
Tim Peters2344fae2001-01-15 00:50:52 +0000280 name = join(top, name)
Guido van Rossuma490d582001-04-16 18:12:04 +0000281 try:
282 st = os.lstat(name)
283 except os.error:
284 continue
Neal Norwitzec7cf132002-06-06 18:16:14 +0000285 if stat.S_ISDIR(st.st_mode):
Tim Peters2344fae2001-01-15 00:50:52 +0000286 walk(name, func, arg)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000287
288
289# Expand paths beginning with '~' or '~user'.
290# '~' means $HOME; '~user' means that user's home directory.
291# If the path doesn't begin with '~', or if the user or $HOME is unknown,
292# the path is returned unchanged (leaving error reporting to whatever
293# function is called with the expanded path as argument).
294# See also module 'glob' for expansion of *, ? and [...] in pathnames.
295# (A function should also be defined to do full *sh-style environment
296# variable expansion.)
297
298def expanduser(path):
Tim Peters2344fae2001-01-15 00:50:52 +0000299 """Expand ~ and ~user constructions. If user or $HOME is unknown,
Fred Drakec0ab93e2000-09-28 16:22:52 +0000300 do nothing."""
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000301 if not path.startswith('~'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000302 return path
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000303 i = path.find('/', 1)
304 if i < 0:
305 i = len(path)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000306 if i == 1:
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000307 if 'HOME' not in os.environ:
Neal Norwitz609ba812002-09-05 21:08:25 +0000308 import pwd
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000309 userhome = pwd.getpwuid(os.getuid()).pw_dir
Neal Norwitz609ba812002-09-05 21:08:25 +0000310 else:
311 userhome = os.environ['HOME']
Guido van Rossum346f7af1997-12-05 19:04:51 +0000312 else:
313 import pwd
314 try:
315 pwent = pwd.getpwnam(path[1:i])
316 except KeyError:
317 return path
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000318 userhome = pwent.pw_dir
319 if userhome.endswith('/'):
320 i += 1
Guido van Rossum346f7af1997-12-05 19:04:51 +0000321 return userhome + path[i:]
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000322
323
324# Expand paths containing shell variable substitutions.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000325# This expands the forms $variable and ${variable} only.
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000326# Non-existent variables are left unchanged.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000327
328_varprog = None
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000329
330def expandvars(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000331 """Expand shell variables of form $var and ${var}. Unknown variables
Fred Drakec0ab93e2000-09-28 16:22:52 +0000332 are left unchanged."""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000333 global _varprog
334 if '$' not in path:
335 return path
336 if not _varprog:
337 import re
338 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
339 i = 0
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000340 while True:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000341 m = _varprog.search(path, i)
342 if not m:
343 break
344 i, j = m.span(0)
345 name = m.group(1)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000346 if name.startswith('{') and name.endswith('}'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000347 name = name[1:-1]
Raymond Hettinger54f02222002-06-01 14:18:47 +0000348 if name in os.environ:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000349 tail = path[j:]
350 path = path[:i] + os.environ[name]
351 i = len(path)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000352 path += tail
Guido van Rossum346f7af1997-12-05 19:04:51 +0000353 else:
354 i = j
355 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000356
357
358# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
359# It should be understood that this may change the meaning of the path
360# if it contains symbolic links!
361
362def normpath(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000363 """Normalize path, eliminating double slashes, etc."""
Skip Montanaro018dfae2000-07-19 17:09:51 +0000364 if path == '':
365 return '.'
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000366 initial_slashes = path.startswith('/')
367 # POSIX allows one or two initial slashes, but treats three or more
368 # as single slash.
Tim Peters658cba62001-02-09 20:06:00 +0000369 if (initial_slashes and
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000370 path.startswith('//') and not path.startswith('///')):
371 initial_slashes = 2
Fred Drake22fb8392000-09-28 15:04:39 +0000372 comps = path.split('/')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000373 new_comps = []
374 for comp in comps:
375 if comp in ('', '.'):
376 continue
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000377 if (comp != '..' or (not initial_slashes and not new_comps) or
Skip Montanaro018dfae2000-07-19 17:09:51 +0000378 (new_comps and new_comps[-1] == '..')):
379 new_comps.append(comp)
380 elif new_comps:
381 new_comps.pop()
382 comps = new_comps
Fred Drake22fb8392000-09-28 15:04:39 +0000383 path = '/'.join(comps)
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000384 if initial_slashes:
385 path = '/'*initial_slashes + path
Skip Montanaro018dfae2000-07-19 17:09:51 +0000386 return path or '.'
Guido van Rossume294cf61999-01-29 18:05:18 +0000387
388
Guido van Rossume294cf61999-01-29 18:05:18 +0000389def abspath(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000390 """Return an absolute path."""
Guido van Rossume294cf61999-01-29 18:05:18 +0000391 if not isabs(path):
392 path = join(os.getcwd(), path)
393 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000394
395
396# Return a canonical path (i.e. the absolute location of a file on the
397# filesystem).
398
399def realpath(filename):
400 """Return the canonical path of the specified filename, eliminating any
401symbolic links encountered in the path."""
402 filename = abspath(filename)
403
404 bits = ['/'] + filename.split('/')[1:]
405 for i in range(2, len(bits)+1):
406 component = join(*bits[0:i])
407 if islink(component):
408 resolved = os.readlink(component)
409 (dir, file) = split(component)
410 resolved = normpath(join(dir, resolved))
411 newpath = join(*([resolved] + bits[i:]))
412 return realpath(newpath)
Tim Petersb64bec32001-09-18 02:26:39 +0000413
Guido van Rossum83eeef42001-09-17 15:16:09 +0000414 return filename
Mark Hammond8696ebc2002-10-08 02:44:31 +0000415
Just van Rossum2d4e9882003-07-17 15:11:49 +0000416supports_unicode_filenames = False