469,358 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Files, directories and imports - relative to the current directoryonly

ptn
Hello, group.

I can only read files and import modules that are in the same
directory
as the one my script is. Here is a test script (path.py):

import os
import uno # some module I wrote

print list(os.walk('~/hacking/python'))
f = open('~/read/foo.txt')
print f.read()

And here is the output:

Traceback (most recent call last):
File "path.py", line 2, in <module>
import uno
ImportError: No module named uno

If I comment that import, the output becomes this:

[]
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'

(Notice the empty list at the beginning, that would be the output of
os.walk().)

I have added this line to my .bashrc:
export PYTHONPATH=$PYTHONPATH:~/hacking/python
I thought that by doing so all the scripts found in that directory and
all it's children would be available for import, but that doesn't seem
to be the case. As for the other problems, I have no idea.

So, what's wrong here? Maybe there's something I haven't set up?
Mar 25 '08 #1
2 1623
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.

Regards,
Björn

--
BOFH excuse #139:

UBNC (user brain not connected)

Mar 25 '08 #2
En Tue, 25 Mar 2008 15:35:34 -0300, Bjoern Schliessmann
<us**************************@spamgourmet.comescri bió:
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

Mar 25 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Rutger Claes | last post: by
6 posts views Thread by Christopher | last post: by
10 posts views Thread by Martin Ho | last post: by
2 posts views Thread by s99999999s2003 | last post: by
reply views Thread by Kay Schluehr | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.