blob: d65dc757b298e255e96969cd30b89b469de48b8c [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
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000288
289def expandvars(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000290 """Expand shell variables of form $var and ${var}. Unknown variables
Fred Drakec0ab93e2000-09-28 16:22:52 +0000291 are left unchanged."""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000292 global _varprog
293 if '$' not in path:
294 return path
295 if not _varprog:
296 import re
297 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
298 i = 0
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000299 while True:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000300 m = _varprog.search(path, i)
301 if not m:
302 break
303 i, j = m.span(0)
304 name = m.group(1)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000305 if name.startswith('{') and name.endswith('}'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000306 name = name[1:-1]
Raymond Hettinger54f02222002-06-01 14:18:47 +0000307 if name in os.environ:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000308 tail = path[j:]
309 path = path[:i] + os.environ[name]
310 i = len(path)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000311 path += tail
Guido van Rossum346f7af1997-12-05 19:04:51 +0000312 else:
313 i = j
314 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000315
316
317# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
318# It should be understood that this may change the meaning of the path
319# if it contains symbolic links!
320
321def normpath(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000322 """Normalize path, eliminating double slashes, etc."""
Ezio Melottib5689de2010-01-12 03:32:05 +0000323 # Preserve unicode (if path is unicode)
Martin v. Löwised11a5d2012-05-20 10:42:17 +0200324 slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000325 if path == '':
Ezio Melottib5689de2010-01-12 03:32:05 +0000326 return dot
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000327 initial_slashes = path.startswith('/')
328 # POSIX allows one or two initial slashes, but treats three or more
329 # as single slash.
Tim Peters658cba62001-02-09 20:06:00 +0000330 if (initial_slashes and
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000331 path.startswith('//') and not path.startswith('///')):
332 initial_slashes = 2
Fred Drake22fb8392000-09-28 15:04:39 +0000333 comps = path.split('/')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000334 new_comps = []
335 for comp in comps:
336 if comp in ('', '.'):
337 continue
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000338 if (comp != '..' or (not initial_slashes and not new_comps) or
Skip Montanaro018dfae2000-07-19 17:09:51 +0000339 (new_comps and new_comps[-1] == '..')):
340 new_comps.append(comp)
341 elif new_comps:
342 new_comps.pop()
343 comps = new_comps
Ezio Melottib5689de2010-01-12 03:32:05 +0000344 path = slash.join(comps)
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000345 if initial_slashes:
Ezio Melottib5689de2010-01-12 03:32:05 +0000346 path = slash*initial_slashes + path
347 return path or dot
Guido van Rossume294cf61999-01-29 18:05:18 +0000348
349
Guido van Rossume294cf61999-01-29 18:05:18 +0000350def abspath(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000351 """Return an absolute path."""
Guido van Rossume294cf61999-01-29 18:05:18 +0000352 if not isabs(path):
Martin v. Löwised11a5d2012-05-20 10:42:17 +0200353 if isinstance(path, _unicode):
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000354 cwd = os.getcwdu()
355 else:
356 cwd = os.getcwd()
357 path = join(cwd, path)
Guido van Rossume294cf61999-01-29 18:05:18 +0000358 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000359
360
361# Return a canonical path (i.e. the absolute location of a file on the
362# filesystem).
363
364def realpath(filename):
365 """Return the canonical path of the specified filename, eliminating any
366symbolic links encountered in the path."""
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200367 path, ok = _joinrealpath('', filename, {})
368 return abspath(path)
Tim Petersa45cacf2004-08-20 03:47:14 +0000369
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200370# Join two paths, normalizing ang eliminating any symbolic links
371# encountered in the second path.
372def _joinrealpath(path, rest, seen):
373 if isabs(rest):
374 rest = rest[1:]
375 path = sep
376
377 while rest:
378 name, _, rest = rest.partition(sep)
379 if not name or name == curdir:
380 # current dir
381 continue
382 if name == pardir:
383 # parent dir
384 if path:
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200385 path, name = split(path)
386 if name == pardir:
387 path = join(path, pardir, pardir)
Brett Cannonf50299c2004-07-10 22:55:15 +0000388 else:
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200389 path = pardir
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200390 continue
391 newpath = join(path, name)
392 if not islink(newpath):
393 path = newpath
394 continue
395 # Resolve the symbolic link
396 if newpath in seen:
397 # Already seen this path
398 path = seen[newpath]
399 if path is not None:
400 # use cached value
401 continue
402 # The symlink is not resolved, so we must have a symlink loop.
403 # Return already resolved part + rest of the path unchanged.
404 return join(newpath, rest), False
405 seen[newpath] = None # not resolved symlink
406 path, ok = _joinrealpath(path, os.readlink(newpath), seen)
407 if not ok:
408 return join(path, rest), False
409 seen[newpath] = path # resolved symlink
Tim Petersb64bec32001-09-18 02:26:39 +0000410
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200411 return path, True
Tim Petersa45cacf2004-08-20 03:47:14 +0000412
Brett Cannonf50299c2004-07-10 22:55:15 +0000413
Victor Stinner8fc843b2010-09-17 23:35:50 +0000414supports_unicode_filenames = (sys.platform == 'darwin')
Collin Winter6f187742007-03-16 22:16:08 +0000415
416def relpath(path, start=curdir):
417 """Return a relative version of a path"""
418
419 if not path:
420 raise ValueError("no path specified")
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000421
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000422 start_list = [x for x in abspath(start).split(sep) if x]
423 path_list = [x for x in abspath(path).split(sep) if x]
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000424
Collin Winter6f187742007-03-16 22:16:08 +0000425 # Work out how much of the filepath is shared by start and path.
426 i = len(commonprefix([start_list, path_list]))
427
428 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
Georg Brandl183a0842008-01-06 14:27:15 +0000429 if not rel_list:
430 return curdir
Collin Winter6f187742007-03-16 22:16:08 +0000431 return join(*rel_list)