Files, directories and imports - relative to the current directoryonly

Discussion in 'Python' started by ptn, Mar 25, 2008.

  1. ptn

    ptn Guest

    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?
     
    ptn, Mar 25, 2008
    #1
    1. Advertising

  2. Re: Files, directories and imports - relative to the current directory only

    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)
     
    Bjoern Schliessmann, Mar 25, 2008
    #2
    1. Advertising

  3. Re: Files,directories and imports - relative to the current directory only

    En Tue, 25 Mar 2008 15:35:34 -0300, Bjoern Schliessmann
    <> 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
     
    Gabriel Genellina, Mar 25, 2008
    #3
  4. ptn

    Guest

    Re: Files, directories and imports - relative to the currentdirectory only

    On Mar 25, 11:27 am, ptn <> 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
     
    , Mar 26, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Albert
    Replies:
    4
    Views:
    10,899
    Albert
    Jul 10, 2008
  2. zildjohn01
    Replies:
    0
    Views:
    656
    zildjohn01
    Feb 22, 2011
  3. Adam Petrie
    Replies:
    8
    Views:
    310
    Adam Petrie
    Oct 11, 2004
  4. Victor Hooi
    Replies:
    1
    Views:
    114
    Devin Jeanpierre
    Nov 25, 2013
  5. Replies:
    0
    Views:
    80
Loading...

Share This Page