blob: 8f928225e0191effb56d51d79793560f4e695fac [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 *
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +020019from genericpath import _unicode
Martin v. Löwised11a5d2012-05-20 10:42:17 +020020
Skip Montanaroc62c81e2001-02-12 02:00:42 +000021__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
22 "basename","dirname","commonprefix","getsize","getmtime",
Georg Brandlf0de6a12005-08-22 18:02:59 +000023 "getatime","getctime","islink","exists","lexists","isdir","isfile",
24 "ismount","walk","expanduser","expandvars","normpath","abspath",
Neal Norwitz61cdac62003-01-03 18:01:57 +000025 "samefile","sameopenfile","samestat",
Skip Montanaro117910d2003-02-14 19:35:31 +000026 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
Collin Winter6f187742007-03-16 22:16:08 +000027 "devnull","realpath","supports_unicode_filenames","relpath"]
Guido van Rossumc6360141990-10-13 19:23:40 +000028
Skip Montanaro117910d2003-02-14 19:35:31 +000029# strings representing various path-related bits and pieces
30curdir = '.'
31pardir = '..'
32extsep = '.'
33sep = '/'
34pathsep = ':'
35defpath = ':/bin:/usr/bin'
36altsep = None
Martin v. Löwisbdec50f2004-06-08 08:29:33 +000037devnull = '/dev/null'
Skip Montanaro117910d2003-02-14 19:35:31 +000038
Guido van Rossum7ac48781992-01-14 18:29:32 +000039# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
40# On MS-DOS this may also turn slashes into backslashes; however, other
41# normalizations (such as optimizing '../' away) are not allowed
42# (another function should be defined to do that).
43
44def normcase(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000045 """Normalize case of pathname. Has no effect under Posix"""
46 return s
Guido van Rossum7ac48781992-01-14 18:29:32 +000047
48
Jeremy Hyltona05e2932000-06-28 14:48:01 +000049# Return whether a path is absolute.
Guido van Rossum7ac48781992-01-14 18:29:32 +000050# Trivial in Posix, harder on the Mac or MS-DOS.
51
52def isabs(s):
Guido van Rossum346f7af1997-12-05 19:04:51 +000053 """Test whether a path is absolute"""
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000054 return s.startswith('/')
Guido van Rossum7ac48781992-01-14 18:29:32 +000055
56
Barry Warsaw384d2491997-02-18 21:53:25 +000057# Join pathnames.
58# Ignore the previous parts if a part is absolute.
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000059# Insert a '/' unless the first part is empty or already ends in '/'.
Guido van Rossum7ac48781992-01-14 18:29:32 +000060
Barry Warsaw384d2491997-02-18 21:53:25 +000061def join(a, *p):
Georg Brandlda5f16a2007-08-23 21:27:57 +000062 """Join two or more pathname components, inserting '/' as needed.
63 If any component is an absolute path, all previous path components
R David Murrayac9b5c62012-07-21 14:37:29 -040064 will be discarded. An empty last part will result in a path that
65 ends with a separator."""
Guido van Rossum346f7af1997-12-05 19:04:51 +000066 path = a
67 for b in p:
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000068 if b.startswith('/'):
Guido van Rossum346f7af1997-12-05 19:04:51 +000069 path = b
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000070 elif path == '' or path.endswith('/'):
71 path += b
Guido van Rossum346f7af1997-12-05 19:04:51 +000072 else:
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000073 path += '/' + b
Guido van Rossum346f7af1997-12-05 19:04:51 +000074 return path
Guido van Rossumc6360141990-10-13 19:23:40 +000075
76
Guido van Rossum26847381992-03-31 18:54:35 +000077# Split a path in head (everything up to the last '/') and tail (the
Guido van Rossuma89b1ba1995-09-01 20:32:21 +000078# rest). If the path ends in '/', tail will be empty. If there is no
79# '/' in the path, head will be empty.
80# Trailing '/'es are stripped from head unless it is the root.
Guido van Rossum7ac48781992-01-14 18:29:32 +000081
Guido van Rossumc6360141990-10-13 19:23:40 +000082def split(p):
Tim Peters2344fae2001-01-15 00:50:52 +000083 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
Fred Drakec0ab93e2000-09-28 16:22:52 +000084 everything after the final slash. Either part may be empty."""
Fred Drake22fb8392000-09-28 15:04:39 +000085 i = p.rfind('/') + 1
Guido van Rossum346f7af1997-12-05 19:04:51 +000086 head, tail = p[:i], p[i:]
Fred Drake8152d322000-12-12 23:20:45 +000087 if head and head != '/'*len(head):
Walter Dörwald77cdeaf2003-06-17 13:13:40 +000088 head = head.rstrip('/')
Guido van Rossum346f7af1997-12-05 19:04:51 +000089 return head, tail
Guido van Rossumc6360141990-10-13 19:23:40 +000090
91
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000092# Split a path in root and extension.
Guido van Rossum422869a1996-08-20 20:24:17 +000093# The extension is everything starting at the last dot in the last
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000094# pathname component; the root is everything before that.
Guido van Rossum7ac48781992-01-14 18:29:32 +000095# It is always true that root + ext == p.
96
Guido van Rossum4d0fdc31991-08-16 13:27:58 +000097def splitext(p):
Martin v. Löwis05c075d2007-03-07 11:04:33 +000098 return genericpath._splitext(p, sep, altsep, extsep)
99splitext.__doc__ = genericpath._splitext.__doc__
Guido van Rossum4d0fdc31991-08-16 13:27:58 +0000100
Guido van Rossum221df241995-08-07 20:17:55 +0000101# Split a pathname into a drive specification and the rest of the
102# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
103
104def splitdrive(p):
Tim Peters2344fae2001-01-15 00:50:52 +0000105 """Split a pathname into drive and path. On Posix, drive is always
Fred Drakec0ab93e2000-09-28 16:22:52 +0000106 empty."""
Guido van Rossum346f7af1997-12-05 19:04:51 +0000107 return '', p
Guido van Rossum221df241995-08-07 20:17:55 +0000108
109
Georg Brandl65ad0432006-10-12 13:08:16 +0000110# Return the tail (basename) part of a path, same as split(path)[1].
Guido van Rossum7ac48781992-01-14 18:29:32 +0000111
Guido van Rossumc6360141990-10-13 19:23:40 +0000112def basename(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000113 """Returns the final component of a pathname"""
Georg Brandl65ad0432006-10-12 13:08:16 +0000114 i = p.rfind('/') + 1
115 return p[i:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000116
117
Georg Brandl65ad0432006-10-12 13:08:16 +0000118# Return the head (dirname) part of a path, same as split(path)[0].
Guido van Rossumc629d341992-11-05 10:43:02 +0000119
120def dirname(p):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000121 """Returns the directory component of a pathname"""
Georg Brandl65ad0432006-10-12 13:08:16 +0000122 i = p.rfind('/') + 1
123 head = p[:i]
124 if head and head != '/'*len(head):
125 head = head.rstrip('/')
126 return head
Guido van Rossumc629d341992-11-05 10:43:02 +0000127
128
Guido van Rossum7ac48781992-01-14 18:29:32 +0000129# Is a path a symbolic link?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000130# This will always return false on systems where os.lstat doesn't exist.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000131
132def islink(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000133 """Test whether a path is a symbolic link"""
134 try:
135 st = os.lstat(path)
136 except (os.error, AttributeError):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000137 return False
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000138 return stat.S_ISLNK(st.st_mode)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000139
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000140# Being true for dangling symbolic links is also useful.
141
142def lexists(path):
143 """Test whether a path exists. Returns True for broken symbolic links"""
144 try:
Georg Brandl84fedf72010-02-06 22:59:15 +0000145 os.lstat(path)
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000146 except os.error:
147 return False
148 return True
149
150
Guido van Rossumd3778f91991-11-12 15:37:40 +0000151# Are two filenames really pointing to the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000152
Guido van Rossumd3778f91991-11-12 15:37:40 +0000153def samefile(f1, f2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000154 """Test whether two pathnames reference the same actual file"""
155 s1 = os.stat(f1)
156 s2 = os.stat(f2)
157 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000158
159
160# Are two open files really referencing the same file?
161# (Not necessarily the same file descriptor!)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000162
Guido van Rossumd3778f91991-11-12 15:37:40 +0000163def sameopenfile(fp1, fp2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000164 """Test whether two open file objects reference the same file"""
165 s1 = os.fstat(fp1)
166 s2 = os.fstat(fp2)
167 return samestat(s1, s2)
Guido van Rossumd3778f91991-11-12 15:37:40 +0000168
169
170# Are two stat buffers (obtained from stat, fstat or lstat)
171# describing the same file?
Guido van Rossum7ac48781992-01-14 18:29:32 +0000172
Guido van Rossumd3778f91991-11-12 15:37:40 +0000173def samestat(s1, s2):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000174 """Test whether two stat buffers reference the same file"""
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000175 return s1.st_ino == s2.st_ino and \
176 s1.st_dev == s2.st_dev
Guido van Rossumc6360141990-10-13 19:23:40 +0000177
178
179# Is a path a mount point?
Guido van Rossumd3876d31996-07-23 03:47:28 +0000180# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000181
Guido van Rossumc6360141990-10-13 19:23:40 +0000182def ismount(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000183 """Test whether a path is a mount point"""
Georg Brandl78e69572010-08-01 18:52:52 +0000184 if islink(path):
185 # A symlink can never be a mount point
186 return False
Guido van Rossum346f7af1997-12-05 19:04:51 +0000187 try:
Christian Heimes06875612008-01-04 13:21:07 +0000188 s1 = os.lstat(path)
189 s2 = os.lstat(join(path, '..'))
Guido van Rossum346f7af1997-12-05 19:04:51 +0000190 except os.error:
Tim Petersbc0e9102002-04-04 22:55:58 +0000191 return False # It doesn't exist -- so not a mount point :-)
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000192 dev1 = s1.st_dev
193 dev2 = s2.st_dev
Guido van Rossum346f7af1997-12-05 19:04:51 +0000194 if dev1 != dev2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000195 return True # path/.. on a different device as path
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000196 ino1 = s1.st_ino
197 ino2 = s2.st_ino
Guido van Rossum346f7af1997-12-05 19:04:51 +0000198 if ino1 == ino2:
Tim Petersbc0e9102002-04-04 22:55:58 +0000199 return True # path/.. is the same i-node as path
200 return False
Guido van Rossumc6360141990-10-13 19:23:40 +0000201
202
203# Directory tree walk.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000204# For each directory under top (including top itself, but excluding
205# '.' and '..'), func(arg, dirname, filenames) is called, where
206# dirname is the name of the directory and filenames is the list
Guido van Rossum346f7af1997-12-05 19:04:51 +0000207# of files (and subdirectories etc.) in the directory.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000208# The func may modify the filenames list, to implement a filter,
Guido van Rossumc6360141990-10-13 19:23:40 +0000209# or to impose a different order of visiting.
Guido van Rossum7ac48781992-01-14 18:29:32 +0000210
Guido van Rossumc6360141990-10-13 19:23:40 +0000211def walk(top, func, arg):
Tim Peterscf5e6a42001-10-10 04:16:20 +0000212 """Directory tree walk with callback function.
213
214 For each directory in the directory tree rooted at top (including top
215 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
216 dirname is the name of the directory, and fnames a list of the names of
217 the files and subdirectories in dirname (excluding '.' and '..'). func
218 may modify the fnames list in-place (e.g. via del or slice assignment),
219 and walk will only recurse into the subdirectories whose names remain in
220 fnames; this can be used to implement a filter, or to impose a specific
221 order of visiting. No semantics are defined for, or required of, arg,
222 beyond that arg is always passed to func. It can be used, e.g., to pass
223 a filename pattern, or a mutable object designed to accumulate
224 statistics. Passing None for arg is common."""
Philip Jenveyd846f1d2009-05-08 02:28:39 +0000225 warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.",
226 stacklevel=2)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000227 try:
228 names = os.listdir(top)
229 except os.error:
230 return
231 func(arg, top, names)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000232 for name in names:
Tim Peters2344fae2001-01-15 00:50:52 +0000233 name = join(top, name)
Guido van Rossuma490d582001-04-16 18:12:04 +0000234 try:
235 st = os.lstat(name)
236 except os.error:
237 continue
Neal Norwitzec7cf132002-06-06 18:16:14 +0000238 if stat.S_ISDIR(st.st_mode):
Tim Peters2344fae2001-01-15 00:50:52 +0000239 walk(name, func, arg)
Guido van Rossum7ac48781992-01-14 18:29:32 +0000240
241
242# Expand paths beginning with '~' or '~user'.
243# '~' means $HOME; '~user' means that user's home directory.
244# If the path doesn't begin with '~', or if the user or $HOME is unknown,
245# the path is returned unchanged (leaving error reporting to whatever
246# function is called with the expanded path as argument).
247# See also module 'glob' for expansion of *, ? and [...] in pathnames.
248# (A function should also be defined to do full *sh-style environment
249# variable expansion.)
250
251def expanduser(path):
Tim Peters2344fae2001-01-15 00:50:52 +0000252 """Expand ~ and ~user constructions. If user or $HOME is unknown,
Fred Drakec0ab93e2000-09-28 16:22:52 +0000253 do nothing."""
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000254 if not path.startswith('~'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000255 return path
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000256 i = path.find('/', 1)
257 if i < 0:
258 i = len(path)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000259 if i == 1:
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000260 if 'HOME' not in os.environ:
Neal Norwitz609ba812002-09-05 21:08:25 +0000261 import pwd
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000262 userhome = pwd.getpwuid(os.getuid()).pw_dir
Neal Norwitz609ba812002-09-05 21:08:25 +0000263 else:
264 userhome = os.environ['HOME']
Guido van Rossum346f7af1997-12-05 19:04:51 +0000265 else:
266 import pwd
267 try:
268 pwent = pwd.getpwnam(path[1:i])
269 except KeyError:
270 return path
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000271 userhome = pwent.pw_dir
Jesus Ceaf2011e32012-05-10 05:01:11 +0200272 userhome = userhome.rstrip('/')
273 return (userhome + path[i:]) or '/'
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000274
275
276# Expand paths containing shell variable substitutions.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277# This expands the forms $variable and ${variable} only.
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000278# Non-existent variables are left unchanged.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279
280_varprog = None
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200281_uvarprog = None
Guido van Rossum4732ccf1992-08-09 13:54:50 +0000282
283def expandvars(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000284 """Expand shell variables of form $var and ${var}. Unknown variables
Fred Drakec0ab93e2000-09-28 16:22:52 +0000285 are left unchanged."""
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200286 global _varprog, _uvarprog
Guido van Rossum346f7af1997-12-05 19:04:51 +0000287 if '$' not in path:
288 return path
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200289 if isinstance(path, _unicode):
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +0200290 if not _uvarprog:
291 import re
292 _uvarprog = re.compile(ur'\$(\w+|\{[^}]*\})', re.UNICODE)
293 varprog = _uvarprog
294 encoding = sys.getfilesystemencoding()
295 else:
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200296 if not _varprog:
297 import re
298 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
299 varprog = _varprog
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200300 encoding = None
Guido van Rossum346f7af1997-12-05 19:04:51 +0000301 i = 0
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000302 while True:
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200303 m = varprog.search(path, i)
Guido van Rossum346f7af1997-12-05 19:04:51 +0000304 if not m:
305 break
306 i, j = m.span(0)
307 name = m.group(1)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000308 if name.startswith('{') and name.endswith('}'):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000309 name = name[1:-1]
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200310 if encoding:
311 name = name.encode(encoding)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000312 if name in os.environ:
Guido van Rossum346f7af1997-12-05 19:04:51 +0000313 tail = path[j:]
Serhiy Storchaka2ac9d312014-02-19 23:27:37 +0200314 value = os.environ[name]
315 if encoding:
316 value = value.decode(encoding)
317 path = path[:i] + value
Guido van Rossum346f7af1997-12-05 19:04:51 +0000318 i = len(path)
Walter Dörwald77cdeaf2003-06-17 13:13:40 +0000319 path += tail
Guido van Rossum346f7af1997-12-05 19:04:51 +0000320 else:
321 i = j
322 return path
Guido van Rossumc629d341992-11-05 10:43:02 +0000323
324
325# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
326# It should be understood that this may change the meaning of the path
327# if it contains symbolic links!
328
329def normpath(path):
Guido van Rossum346f7af1997-12-05 19:04:51 +0000330 """Normalize path, eliminating double slashes, etc."""
Ezio Melottib5689de2010-01-12 03:32:05 +0000331 # Preserve unicode (if path is unicode)
Martin v. Löwised11a5d2012-05-20 10:42:17 +0200332 slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000333 if path == '':
Ezio Melottib5689de2010-01-12 03:32:05 +0000334 return dot
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000335 initial_slashes = path.startswith('/')
336 # POSIX allows one or two initial slashes, but treats three or more
337 # as single slash.
Tim Peters658cba62001-02-09 20:06:00 +0000338 if (initial_slashes and
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000339 path.startswith('//') and not path.startswith('///')):
340 initial_slashes = 2
Fred Drake22fb8392000-09-28 15:04:39 +0000341 comps = path.split('/')
Skip Montanaro018dfae2000-07-19 17:09:51 +0000342 new_comps = []
343 for comp in comps:
344 if comp in ('', '.'):
345 continue
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000346 if (comp != '..' or (not initial_slashes and not new_comps) or
Skip Montanaro018dfae2000-07-19 17:09:51 +0000347 (new_comps and new_comps[-1] == '..')):
348 new_comps.append(comp)
349 elif new_comps:
350 new_comps.pop()
351 comps = new_comps
Ezio Melottib5689de2010-01-12 03:32:05 +0000352 path = slash.join(comps)
Marc-André Lemburgbf222c92001-01-29 11:29:44 +0000353 if initial_slashes:
Ezio Melottib5689de2010-01-12 03:32:05 +0000354 path = slash*initial_slashes + path
355 return path or dot
Guido van Rossume294cf61999-01-29 18:05:18 +0000356
357
Guido van Rossume294cf61999-01-29 18:05:18 +0000358def abspath(path):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000359 """Return an absolute path."""
Guido van Rossume294cf61999-01-29 18:05:18 +0000360 if not isabs(path):
Martin v. Löwised11a5d2012-05-20 10:42:17 +0200361 if isinstance(path, _unicode):
Ezio Melotti4cc80ca2010-02-20 08:09:39 +0000362 cwd = os.getcwdu()
363 else:
364 cwd = os.getcwd()
365 path = join(cwd, path)
Guido van Rossume294cf61999-01-29 18:05:18 +0000366 return normpath(path)
Guido van Rossum83eeef42001-09-17 15:16:09 +0000367
368
369# Return a canonical path (i.e. the absolute location of a file on the
370# filesystem).
371
372def realpath(filename):
373 """Return the canonical path of the specified filename, eliminating any
374symbolic links encountered in the path."""
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200375 path, ok = _joinrealpath('', filename, {})
376 return abspath(path)
Tim Petersa45cacf2004-08-20 03:47:14 +0000377
Martin Panterbf02d182016-04-16 09:28:57 +0000378# Join two paths, normalizing and eliminating any symbolic links
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200379# encountered in the second path.
380def _joinrealpath(path, rest, seen):
381 if isabs(rest):
382 rest = rest[1:]
383 path = sep
384
385 while rest:
386 name, _, rest = rest.partition(sep)
387 if not name or name == curdir:
388 # current dir
389 continue
390 if name == pardir:
391 # parent dir
392 if path:
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200393 path, name = split(path)
394 if name == pardir:
395 path = join(path, pardir, pardir)
Brett Cannonf50299c2004-07-10 22:55:15 +0000396 else:
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200397 path = pardir
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200398 continue
399 newpath = join(path, name)
400 if not islink(newpath):
401 path = newpath
402 continue
403 # Resolve the symbolic link
404 if newpath in seen:
405 # Already seen this path
406 path = seen[newpath]
407 if path is not None:
408 # use cached value
409 continue
410 # The symlink is not resolved, so we must have a symlink loop.
411 # Return already resolved part + rest of the path unchanged.
412 return join(newpath, rest), False
413 seen[newpath] = None # not resolved symlink
414 path, ok = _joinrealpath(path, os.readlink(newpath), seen)
415 if not ok:
416 return join(path, rest), False
417 seen[newpath] = path # resolved symlink
Tim Petersb64bec32001-09-18 02:26:39 +0000418
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200419 return path, True
Tim Petersa45cacf2004-08-20 03:47:14 +0000420
Brett Cannonf50299c2004-07-10 22:55:15 +0000421
Victor Stinner8fc843b2010-09-17 23:35:50 +0000422supports_unicode_filenames = (sys.platform == 'darwin')
Collin Winter6f187742007-03-16 22:16:08 +0000423
424def relpath(path, start=curdir):
425 """Return a relative version of a path"""
426
427 if not path:
428 raise ValueError("no path specified")
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000429
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000430 start_list = [x for x in abspath(start).split(sep) if x]
431 path_list = [x for x in abspath(path).split(sep) if x]
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000432
Collin Winter6f187742007-03-16 22:16:08 +0000433 # Work out how much of the filepath is shared by start and path.
434 i = len(commonprefix([start_list, path_list]))
435
436 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
Georg Brandl183a0842008-01-06 14:27:15 +0000437 if not rel_list:
438 return curdir
Collin Winter6f187742007-03-16 22:16:08 +0000439 return join(*rel_list)