Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Files, directories and imports - relative to the current directoryonly

Reply
Thread Tools

Files, directories and imports - relative to the current directoryonly

 
 
ptn
Guest
Posts: n/a
 
      03-25-2008
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?
 
Reply With Quote
 
 
 
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      03-25-2008
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)

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-25-2008
En Tue, 25 Mar 2008 15:35:34 -0300, Bjoern Schliessmann
<(E-Mail Removed)> 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

 
Reply With Quote
 
ptrk.mcm@gmail.com
Guest
Posts: n/a
 
      03-26-2008
On Mar 25, 11:27 am, ptn <(E-Mail Removed)> wrote:
> 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.


i'm not sure why you are unable to import uno (assuming uno is at ~/
hacking/python/uno.py) after exporting the PYTHONPATH variable. an
alternative way to incorporate uno is to change sys.path, the list of
search paths

import sys
sys.path.append(os.path.expanduser('~/hacking/python'))
import uno

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Relative imports and "import X as Y" OKB (not okblacke) Python 4 09-02-2008 05:40 PM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
Fun with relative imports and py3k Kay Schluehr Python 0 04-25-2008 04:05 PM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM



Advertisments