opening a file

Chris Rebert clp2 at rebertia.com
Mon Jun 20 02:30:34 EDT 2011


On Sun, Jun 19, 2011 at 11:00 PM, Tim Hanson <tjhanson at yahoo.com> wrote:
> Using linux and Python 2.6, learning how to work with files from a Windows
> oriented textbook:
>
> This works:
> infile=open('/foo/bar/prog/py_modules/this_is_a_test','r')
>
> This doesn't:
> infile=open('~/prog/py_modules/this_is_a_test','r')
>
> Can't I work with files using Unix expressions?

As Dennis explained, no. At least, not directly. Python doesn't run
filepaths through the shell for expansion.

However, you can use os.path.expanduser() to make ~-paths work:
http://docs.python.org/library/os.path.html#os.path.expanduser

Cheers,
Chris



More information about the Python-list mailing list