Files, directories and imports - relative to the current directory only

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at
Tue Mar 25 19:35:34 CET 2008

ptn wrote:
>         Traceback (most recent call last):
>           File "", 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.



BOFH excuse #139:

UBNC (user brain not connected)

More information about the Python-list mailing list