[Python-Dev] Add os.path.resolve to simplify the use of os.readlink

Armin Ronacher armin.ronacher at active-4.com
Thu Jun 21 12:23:25 CEST 2012


Due to an user error on my part I was not using os.readlink correctly. 
Since links can be relative to their location I think it would make sense
to provide an os.path.resolve helper that automatically returns the
absolute path:

    def resolve(filename):
        try:
            target = os.readlink(filename)
        except OSError as e:
            if e.errno == errno.EINVAL:
                return abspath(filename)
            raise
        return normpath(join(dirname(filename), target))

The above implementation also does not fail if an entity exists but is not
a link and just returns the absolute path of the given filename in that
case.


Regards,
Armin



More information about the Python-Dev mailing list