[Python-bugs-list] [ python-Bugs-532687 ] "realpath" for posixpath module

noreply@sourceforge.net noreply@sourceforge.net
Wed, 20 Mar 2002 14:00:05 -0800


Bugs item #532687, was opened at 2002-03-20 14:45
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=532687&group_id=5470

Category: Python Library
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Chris McDonough (chrism)
Assigned to: Nobody/Anonymous (nobody)
>Summary: "realpath" for posixpath module

Initial Comment:
It would be nice to be able to have a function which 
resolves a path to its "real" path (removing any 
symlinks in the path) available from the posixpath 
library.  Below (and attached, in case the rendering 
gets munged by SF) is a Python implementation that 
might be useful.  It may be better to just wrap the 
realpath UNIX API call, but I'm not sure whether there 
are cross-platform nuances to it.

I suppose this could go in to Jeremy's Small Features 
PEP.

import os

def realpath(p):
    p = os.path.abspath(p)
    path_list = p.split(os.sep)
    orig_len = len(path_list)
    changed = 0
    i = 1
    while not changed and i < orig_len:
        head = path_list[:i]
        tail = path_list[i:]
        head_s = os.sep.join(head)
        tail_s = os.sep.join(tail)
        if os.path.islink(head_s):
            head_s = os.readlink(head_s)
            path_list = head_s.split(os.sep)
            path_list.extend(tail)
            p = os.sep.join(path_list)
            p = realpath(p)
            changed = 1
        i = i + 1
    return p


----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-03-20 17:00

Message:
Logged In: YES 
user_id=6380

Eh, Chris, in Python 2.2 there already *is* a realpath()
which seems to do what you want. You can close the request
if you agree that's good enough -- if not, I'd like to hear
about where it's lacking. :-)

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=532687&group_id=5470