blob: 037800418df67a990d3a52fe6a36b7d13da51fc0 [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
Hirokazu Yamamotoc3937f52010-09-18 05:40:44 +000014import sys
Guido van Rossum40d93041990-10-21 16:17:34 +000015import stat
Martin v. Löwis05c075d2007-03-07 11:04:33 +000016import genericpath
Benjamin Peterson0893a0a2008-05-09 00:27:01 +000017import warnings
Jack Diederich7b604642006-08-26 18:42:06 +000018from genericpath import *
Guido van Rossumc6360141990-10-13 19:23:40 +000019
Martin v. Löwised11a5d2012-05-20 10:42:17 +020020try:
21 _unicode = unicode
22except NameError:
23 # If Python is built without Unicode support, the unicode type
24 # will not exist. Fake one.
25 class _unicode(object):
26 pass
27
Skip Montanaroc62c81e2001-02-12 02:00:42 +000028__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
29 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000030 "getatime","getctime","islink","exists","lexists","isdir","isfile",
31 "ismount","walk","expanduser","expandvars","normpath","abspath",
Neal Norwitz61cdac62003-01-03 18:01:57 +000032 "samefile","sameopenfile","samestat",
Skip Montanaro117910d2003-02-14 19:35:31 +000033 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
Collin Winter6f187742007-03-16 22:16:08 +000034 "devnull","realpath","supports_unicode_filenames","relpath"]
Guido van Rossumc6360141990-10-13 19:23:40 +000035
Skip Montanaro117910d2003-02-14 19:35:31 +000036# strings representing various path-related bits and pieces
37curdir = '.'
38pardir = '..'
39extsep = '.'
40sep = '/'
41pathsep = ':'
42defpath = ':/bin:/usr/bin'
43altsep = None
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000044devnull = '/dev/null'
Skip Montanaro117910d2003-02-14 19:35:31 +000045
Guido van Rossum7ac48781992-01-14 18:29:32 +000046# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
47# On MS-DOS this may also turn slashes into backslashes; however, other
48# normalizations (such as optimizing '../' away) are not allowed
49# (another function should be defined to do that).
50
51def normcase(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000052 """Normalize case of pathname. Has no effect under Posix"""
53 return s
Guido van Rossum7ac48781992-01-14 18:29:32 +000054
55
Jeremy Hyltona05e2932000-06-28 14:48:01 +000056# Return whether a path is absolute.
Guido van Rossum7ac48781992-01-14 18:29:32 +000057# Trivial in Posix, harder on the Mac or MS-DOS.
58
59def isabs(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000060 """Test whether a path is absolute"""
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000061 return s.startswith('/')
Guido van Rossum7ac48781992-01-14 18:29:32 +000062
63
Barry Warsaw384d2491997-02-18 21:53:25 +000064# Join pathnames.
65# Ignore the previous parts if a part is absolute.
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000066# Insert a '/' unless the first part is empty or already ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000067
Barry Warsaw384d2491997-02-18 21:53:25 +000068def join(a, *p):
Georg Brandlda5f16a2007-08-23 21:27:57 +000069 """Join two or more pathname components, inserting '/' as needed.
70 If any component is an absolute path, all previous path components
R David Murrayac9b5c62012-07-21 14:37:29 -040071 will be discarded. An empty last part will result in a path that
72 ends with a separator."""
Guido van Rossum346f7af1997-12-05 19:04:51 +000073 path = a
74 for b in p:
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000075 if b.startswith('/'):
Guido van Rossum346f7af1997-12-05 19:04:51 +000076 path = b
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000077 elif path == '' or path.endswith('/'):
78 path += b
Guido van Rossum346f7af1997-12-05 19:04:51 +000079 else:
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000080 path += '/' + b
Guido van Rossum346f7af1997-12-05 19:04:51 +000081 return path
Guido van Rossumc6360141990-10-13 19:23:40 +000082
83
Guido van Rossum26847381992-03-31 18:54:35 +000084# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000085# rest). If the path ends in '/', tail will be empty. If there is no
86# '/' in the path, head will be empty.
87# Trailing '/'es are stripped from head unless it is the root.
Guido van Rossum7ac48781992-01-14 18:29:32 +000088
Guido van Rossumc6360141990-10-13 19:23:40 +000089def split(p):
Tim Peters2344fae2001-01-15 00:50:52 +000090 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
Fred Drakec0ab93e2000-09-28 16:22:52 +000091 everything after the final slash. Either part may be empty."""
Fred Drake22fb8392000-09-28 15:04:39 +000092 i = p.rfind('/') + 1
Guido van Rossum346f7af1997-12-05 19:04:51 +000093 head, tail = p[:i], p[i:]
Fred Drake8152d322000-12-12 23:20:45 +000094 if head and head != '/'*len(head):
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000095 head = head.rstrip('/')
Guido van Rossum346f7af1997-12-05 19:04:51 +000096 return head, tail
Guido van Rossumc6360141990-10-13 19:23:40 +000097
98
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000099# Split a path in root and extension.
Guido van Rossum422869a1996-08-20 20:24:17 +0000100# The extension is everything starting at the last dot in the last
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000101# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000102# It is always true that root + ext == p.
103
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000104def splitext(p):
Martin v. Löwis05c075d2007-03-07 11:04:33 +0000105 return genericpath._splitext(p, sep, altsep, extsep)
106splitext.__doc__ = genericpath._splitext.__doc__
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000107
Guido van Rossum221df241995-08-07 20:17:55 +0000108# Split a pathname into a drive specification and the rest of the
109# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
110
111def splitdrive(p):
Tim Peters2344fae2001-01-15 00:50:52 +0000112 """Split a pathname into drive and path. On Posix, drive is always
Fred Drakec0ab93e2000-09-28 16:22:52 +0000113 empty."""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000114 return '', p
Guido van Rossum221df241995-08-07 20:17:55 +0000115
116
Georg Brandl65ad0432006-10-12 13:08:16 +0000117# Return the tail (basename) part of a path, same as split(path)[1].
Guido van Rossum7ac48781992-01-14 18:29:32 +0000118
Guido van Rossumc6360141990-10-13 19:23:40 +0000119def basename(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000120 """Returns the final component of a pathname"""
Georg Brandl65ad0432006-10-12 13:08:16 +0000121 i = p.rfind('/') + 1
122 return p[i:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000123
124
Georg Brandl65ad0432006-10-12 13:08:16 +0000125# Return the head (dirname) part of a path, same as split(path)[0].
Guido van Rossumc629d341992-11-05 10:43:02 +0000126
127def dirname(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000128 """Returns the directory component of a pathname"""
Georg Brandl65ad0432006-10-12 13:08:16 +0000129 i = p.rfind('/') + 1
130 head = p[:i]
131 if head and head != '/'*len(head):
132 head = head.rstrip('/')
133 return head
Guido van Rossumc629d341992-11-05 10:43:02 +0000134
135
Guido van Rossum7ac48781992-01-14 18:29:32 +0000136# Is a path a symbolic link?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000137# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000138
139def islink(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000140 """Test whether a path is a symbolic link"""
141 try:
142 st = os.lstat(path)
143 except (os.error, AttributeError):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000144 return False
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000145 return stat.S_ISLNK(st.st_mode)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000146
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000147# Being true for dangling symbolic links is also useful.
148
149def lexists(path):
150 """Test whether a path exists. Returns True for broken symbolic links"""
151 try:
Georg Brandl84fedf72010-02-06 22:59:15 +0000152 os.lstat(path)
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000153 except os.error:
154 return False
155 return True
156
157
Guido van Rossumd3778f91991-11-12 15:37:40 +0000158# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000159
Guido van Rossumd3778f91991-11-12 15:37:40 +0000160def samefile(f1, f2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000161 """Test whether two pathnames reference the same actual file"""
162 s1 = os.stat(f1)
163 s2 = os.stat(f2)
164 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000165
166
167# Are two open files really referencing the same file?
168# (Not necessarily the same file descriptor!)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000169
Guido van Rossumd3778f91991-11-12 15:37:40 +0000170def sameopenfile(fp1, fp2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000171 """Test whether two open file objects reference the same file"""
172 s1 = os.fstat(fp1)
173 s2 = os.fstat(fp2)
174 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000175
176
177# Are two stat buffers (obtained from stat, fstat or lstat)
178# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000179
Guido van Rossumd3778f91991-11-12 15:37:40 +0000180def samestat(s1, s2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000181 """Test whether two stat buffers reference the same file"""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000182 return s1.st_ino == s2.st_ino and \
183 s1.st_dev == s2.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 Rossum346f7af1997-12-05 19:04:51 +0000190 """Test whether a path is a mount point"""
Georg Brandl78e69572010-08-01 18:52:52 +0000191 if islink(path):
192 # A symlink can never be a mount point
193 return False
Guido van Rossum346f7af1997-12-05 19:04:51 +0000194 try:
Christian Heimes06875612008-01-04 13:21:07 +0000195 s1 = os.lstat(path)
196 s2 = os.lstat(join(path, '..'))
Guido van Rossum346f7af1997-12-05 19:04:51 +0000197 except os.error:
Tim Petersbc0e9102002-04-04 22:55:58 +0000198 return False # It doesn't exist -- so not a mount point :-)
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000199 dev1 = s1.st_dev
200 dev2 = s2.st_dev
Guido van Rossum346f7af1997-12-05 19:04:51 +0000201 if dev1 != dev2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000202 return True # path/.. on a different device as path
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000203 ino1 = s1.st_ino
204 ino2 = s2.st_ino
Guido van Rossum346f7af1997-12-05 19:04:51 +0000205 if ino1 == ino2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000206 return True # path/.. is the same i-node as path
207 return False
Guido van Rossumc6360141990-10-13 19:23:40 +0000208
209
210# Directory tree walk.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000211# For each directory under top (including top itself, but excluding
212# '.' and '..'), func(arg, dirname, filenames) is called, where
213# dirname is the name of the directory and filenames is the list
Guido van Rossum346f7af1997-12-05 19:04:51 +0000214# of files (and subdirectories etc.) in the directory.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000215# The func may modify the filenames list, to implement a filter,
Guido van Rossumc6360141990-10-13 19:23:40 +0000216# or to impose a different order of visiting.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000217
Guido van Rossumc6360141990-10-13 19:23:40 +0000218def walk(top, func, arg):
Tim Peterscf5e6a42001-10-10 04:16:20 +0000219 """Directory tree walk with callback function.
220
221 For each directory in the directory tree rooted at top (including top
222 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
223 dirname is the name of the directory, and fnames a list of the names of
224 the files and subdirectories in dirname (excluding '.' and '..'). func
225 may modify the fnames list in-place (e.g. via del or slice assignment),
226 and walk will only recurse into the subdirectories whose names remain in
227 fnames; this can be used to implement a filter, or to impose a specific
228 order of visiting. No semantics are defined for, or required of, arg,
229 beyond that arg is always passed to func. It can be used, e.g., to pass
230 a filename pattern, or a mutable object designed to accumulate
231 statistics. Passing None for arg is common."""
Philip Jenveyd846f1d2009-05-08 02:28:39 +0000232 warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.",
233 stacklevel=2)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000234 try:
235 names = os.listdir(top)
236 except os.error:
237 return
238 func(arg, top, names)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000239 for name in names:
Tim Peters2344fae2001-01-15 00:50:52 +0000240 name = join(top, name)
Guido van Rossuma490d582001-04-16 18:12:04 +0000241 try:
242 st = os.lstat(name)
243 except os.error:
244 continue
Neal Norwitzec7cf132002-06-06 18:16:14 +0000245 if stat.S_ISDIR(st.st_mode):
Tim Peters2344fae2001-01-15 00:50:52 +0000246 walk(name, func, arg)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000247
248
249# Expand paths beginning with '~' or '~user'.
250# '~' means $HOME; '~user' means that user's home directory.
251# If the path doesn't begin with '~', or if the user or $HOME is unknown,
252# the path is returned unchanged (leaving error reporting to whatever
253# function is called with the expanded path as argument).
254# See also module 'glob' for expansion of *, ? and [...] in pathnames.
255# (A function should also be defined to do full *sh-style environment
256# variable expansion.)
257
258def expanduser(path):
Tim Peters2344fae2001-01-15 00:50:52 +0000259 """Expand ~ and ~user constructions. If user or $HOME is unknown,
Fred Drakec0ab93e2000-09-28 16:22:52 +0000260 do nothing."""
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000261 if not path.startswith('~'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000262 return path
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000263 i = path.find('/', 1)
264 if i < 0:
265 i = len(path)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000266 if i == 1:
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000267 if 'HOME' not in os.environ:
Neal Norwitz609ba812002-09-05 21:08:25 +0000268 import pwd
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000269 userhome = pwd.getpwuid(os.getuid()).pw_dir
Neal Norwitz609ba812002-09-05 21:08:25 +0000270 else:
271 userhome = os.environ['HOME']
Guido van Rossum346f7af1997-12-05 19:04:51 +0000272 else:
273 import pwd
274 try:
275 pwent = pwd.getpwnam(path[1:i])
276 except KeyError:
277 return path
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000278 userhome = pwent.pw_dir
Jesus Ceaf2011e32012-05-10 05:01:11 +0200279 userhome = userhome.rstrip('/')
280 return (userhome + path[i:]) or '/'
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000281
282
283# Expand paths containing shell variable substitutions.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284# This expands the forms $variable and ${variable} only.
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000285# Non-existent variables are left unchanged.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286
287_varprog = None
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200288_uvarprog = None
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000289
290def expandvars(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000291 """Expand shell variables of form $var and ${var}. Unknown variables
Fred Drakec0ab93e2000-09-28 16:22:52 +0000292 are left unchanged."""
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200293 global _varprog, _uvarprog
Guido van Rossum346f7af1997-12-05 19:04:51 +0000294 if '$' not in path:
295 return path
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200296 if isinstance(path, _unicode):
297 if not _varprog:
298 import re
299 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
300 varprog = _varprog
301 encoding = sys.getfilesystemencoding()
302 else:
303 if not _uvarprog:
304 import re
305 _uvarprog = re.compile(_unicode(r'\$(\w+|\{[^}]*\})'), re.UNICODE)
306 varprog = _uvarprog
307 encoding = None
Guido van Rossum346f7af1997-12-05 19:04:51 +0000308 i = 0
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000309 while True:
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200310 m = varprog.search(path, i)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000311 if not m:
312 break
313 i, j = m.span(0)
314 name = m.group(1)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000315 if name.startswith('{') and name.endswith('}'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000316 name = name[1:-1]
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200317 if encoding:
318 name = name.encode(encoding)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000319 if name in os.environ:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000320 tail = path[j:]
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200321 value = os.environ[name]
322 if encoding:
323 value = value.decode(encoding)
324 path = path[:i] + value
Guido van Rossum346f7af1997-12-05 19:04:51 +0000325 i = len(path)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000326 path += tail
Guido van Rossum346f7af1997-12-05 19:04:51 +0000327 else:
328 i = j
329 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000330
331
332# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
333# It should be understood that this may change the meaning of the path
334# if it contains symbolic links!
335
336def normpath(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000337 """Normalize path, eliminating double slashes, etc."""
Ezio Melottib5689de2010-01-12 03:32:05 +0000338 # Preserve unicode (if path is unicode)
Martin v. Löwised11a5d2012-05-20 10:42:17 +0200339 slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000340 if path == '':
Ezio Melottib5689de2010-01-12 03:32:05 +0000341 return dot
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000342 initial_slashes = path.startswith('/')
343 # POSIX allows one or two initial slashes, but treats three or more
344 # as single slash.
Tim Peters658cba62001-02-09 20:06:00 +0000345 if (initial_slashes and
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000346 path.startswith('//') and not path.startswith('///')):
347 initial_slashes = 2
Fred Drake22fb8392000-09-28 15:04:39 +0000348 comps = path.split('/')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000349 new_comps = []
350 for comp in comps:
351 if comp in ('', '.'):
352 continue
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000353 if (comp != '..' or (not initial_slashes and not new_comps) or
Skip Montanaro018dfae2000-07-19 17:09:51 +0000354 (new_comps and new_comps[-1] == '..')):
355 new_comps.append(comp)
356 elif new_comps:
357 new_comps.pop()
358 comps = new_comps
Ezio Melottib5689de2010-01-12 03:32:05 +0000359 path = slash.join(comps)
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000360 if initial_slashes:
Ezio Melottib5689de2010-01-12 03:32:05 +0000361 path = slash*initial_slashes + path
362 return path or dot
Guido van Rossume294cf61999-01-29 18:05:18 +0000363
364
Guido van Rossume294cf61999-01-29 18:05:18 +0000365def abspath(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000366 """Return an absolute path."""
Guido van Rossume294cf61999-01-29 18:05:18 +0000367 if not isabs(path):
Martin v. Löwised11a5d2012-05-20 10:42:17 +0200368 if isinstance(path, _unicode):
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000369 cwd = os.getcwdu()
370 else:
371 cwd = os.getcwd()
372 path = join(cwd, path)
Guido van Rossume294cf61999-01-29 18:05:18 +0000373 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000374
375
376# Return a canonical path (i.e. the absolute location of a file on the
377# filesystem).
378
379def realpath(filename):
380 """Return the canonical path of the specified filename, eliminating any
381symbolic links encountered in the path."""
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200382 path, ok = _joinrealpath('', filename, {})
383 return abspath(path)
Tim Petersa45cacf2004-08-20 03:47:14 +0000384
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200385# Join two paths, normalizing ang eliminating any symbolic links
386# encountered in the second path.
387def _joinrealpath(path, rest, seen):
388 if isabs(rest):
389 rest = rest[1:]
390 path = sep
391
392 while rest:
393 name, _, rest = rest.partition(sep)
394 if not name or name == curdir:
395 # current dir
396 continue
397 if name == pardir:
398 # parent dir
399 if path:
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200400 path, name = split(path)
401 if name == pardir:
402 path = join(path, pardir, pardir)
Brett Cannonf50299c2004-07-10 22:55:15 +0000403 else:
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200404 path = pardir
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200405 continue
406 newpath = join(path, name)
407 if not islink(newpath):
408 path = newpath
409 continue
410 # Resolve the symbolic link
411 if newpath in seen:
412 # Already seen this path
413 path = seen[newpath]
414 if path is not None:
415 # use cached value
416 continue
417 # The symlink is not resolved, so we must have a symlink loop.
418 # Return already resolved part + rest of the path unchanged.
419 return join(newpath, rest), False
420 seen[newpath] = None # not resolved symlink
421 path, ok = _joinrealpath(path, os.readlink(newpath), seen)
422 if not ok:
423 return join(path, rest), False
424 seen[newpath] = path # resolved symlink
Tim Petersb64bec32001-09-18 02:26:39 +0000425
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200426 return path, True
Tim Petersa45cacf2004-08-20 03:47:14 +0000427
Brett Cannonf50299c2004-07-10 22:55:15 +0000428
Victor Stinner8fc843b2010-09-17 23:35:50 +0000429supports_unicode_filenames = (sys.platform == 'darwin')
Collin Winter6f187742007-03-16 22:16:08 +0000430
431def relpath(path, start=curdir):
432 """Return a relative version of a path"""
433
434 if not path:
435 raise ValueError("no path specified")
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000436
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000437 start_list = [x for x in abspath(start).split(sep) if x]
438 path_list = [x for x in abspath(path).split(sep) if x]
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000439
Collin Winter6f187742007-03-16 22:16:08 +0000440 # Work out how much of the filepath is shared by start and path.
441 i = len(commonprefix([start_list, path_list]))
442
443 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
Georg Brandl183a0842008-01-06 14:27:15 +0000444 if not rel_list:
445 return curdir
Collin Winter6f187742007-03-16 22:16:08 +0000446 return join(*rel_list)