Files, directories and imports - relative to the current directory only
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Mar 25 15:52:50 EDT 2008
En Tue, 25 Mar 2008 15:35:34 -0300, Bjoern Schliessmann
<usenet-mail-0306.20.chr0n0ss at spamgourmet.com> escribió:
> ptn wrote:
>> Traceback (most recent call last):
>> File "path.py", line 4, in <module>
>> f = open('~/read/foo.txt')
>> IOError: [Errno 2] No such file or
>> directory: '~/read/foo.txt'
>> [...]
>> So, what's wrong here? Maybe there's something I haven't set up?
>
> Simple: the directory "~" doesn't exist. Since you're not using a
> shell (but direct file access) there is no tilde expansion, and "~"
> is treated as a regular file name. If you need to get the home
> directory refer to the environment variable HOME
> (os.environ["HOME"]). There even may be a shorter way, please refer
> to the os module docs.
That shorter way being os.path.expanduser
http://docs.python.org/lib/module-os.path.html
--
Gabriel Genellina
More information about the Python-list
mailing list