Entry
Expanding a filename
Jul 5th, 2000 10:00
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 154, M.-A. Lemburg
"""
Packages: files;operating_systems.generic
"""
"""
> Is there a module/function that will expand a filename or partial path
> to it's full path?
>
> ie something like this: (assuming program is executed in '/home/me')
>
> f = 'afile'
> exp_f = expand(f)
> print 'filename :',exp_f
>
> ==> filename : /home/me/afile
Try this:
"""
def abspath(path):
import os
try:
path = os.path.expandvars(path)
except AttributeError:
pass
try:
path = os.path.expanduser(path)
except AttributeError:
pass
return os.path.join(os.getcwd(),path)
"""
Example:
>>> abspath('.cshrc')
'/data/home/lemburg/Prometheus/.cshrc'
>>> abspath('$HOME/.cshrc')
'/home/lemburg/.cshrc'
It will apply all the usual shell magic to the given path and
make it absolute. For file globbing (expanding patterns like
*.py) see the standard module glob.
Hmm, maybe a function like this should go in posixpath.py by
default...
"""