Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 83eeef4

Browse files
committed
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Once upon a time, I put together a little function that tries to find the canonical filename for a given pathname on POSIX. I've finally gotten around to turning it into a proper patch with documentation. On non-POSIX, I made it an alias for 'abspath', as that's the behavior on POSIX when no symlinks are encountered in the path. Example: >>> os.path.realpath('/usr/bin/X11/X') '/usr/X11R6/bin/X'
1 parent 3065c94 commit 83eeef4

6 files changed

Lines changed: 41 additions & 0 deletions

File tree

Doc/lib/libposixpath.tex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ \section{\module{os.path} ---
137137
forward slashes to backward slashes.
138138
\end{funcdesc}
139139

140+
\begin{funcdesc}{realpath}{path}
141+
Return the canonical path of the specified filename, eliminating any
142+
symbolic links encountered in the path.
143+
Availability: \UNIX{}.
144+
\versionadded{2.2}
145+
\end{funcdesc}
146+
140147
\begin{funcdesc}{samefile}{path1, path2}
141148
Return true if both pathname arguments refer to the same file or
142149
directory (as indicated by device number and i-node number).

Lib/dospath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,6 @@ def abspath(path):
330330
if not isabs(path):
331331
path = join(os.getcwd(), path)
332332
return normpath(path)
333+
334+
# realpath is a no-op on systems without islink support
335+
realpath = abspath

Lib/macpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,6 @@ def abspath(path):
225225
if not isabs(path):
226226
path = join(os.getcwd(), path)
227227
return normpath(path)
228+
229+
# realpath is a no-op on systems without islink support
230+
realpath = abspath

Lib/ntpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,6 @@ def abspath(path):
451451
else:
452452
path = os.getcwd()
453453
return normpath(path)
454+
455+
# realpath is a no-op on systems without islink support
456+
realpath = abspath

Lib/plat-riscos/riscospath.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ def abspath(p):
315315
return normpath(join(os.getcwd(), p))
316316

317317

318+
# realpath is a no-op on systems without islink support
319+
realpath = abspath
320+
321+
318322
# Normalize a path. Only special path element under RISC OS is "^" for "..".
319323

320324
def normpath(p):

Lib/posixpath.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,24 @@ def abspath(path):
379379
if not isabs(path):
380380
path = join(os.getcwd(), path)
381381
return normpath(path)
382+
383+
384+
# Return a canonical path (i.e. the absolute location of a file on the
385+
# filesystem).
386+
387+
def realpath(filename):
388+
"""Return the canonical path of the specified filename, eliminating any
389+
symbolic links encountered in the path."""
390+
filename = abspath(filename)
391+
392+
bits = ['/'] + filename.split('/')[1:]
393+
for i in range(2, len(bits)+1):
394+
component = join(*bits[0:i])
395+
if islink(component):
396+
resolved = os.readlink(component)
397+
(dir, file) = split(component)
398+
resolved = normpath(join(dir, resolved))
399+
newpath = join(*([resolved] + bits[i:]))
400+
return realpath(newpath)
401+
402+
return filename

0 commit comments

Comments
 (0)