Finding the local directory of a file in the python path

Discussion in 'Python' started by are Dogue, May 18, 2011.

  1. are Dogue

    are Dogue Guest

    Hi there,

    I have a problem that I can't seem to solve after quite a bit of
    searching, probably because I can't find the right terms to search
    for.

    Basically, here's the situation. Let's say I have a file at ~/foo/bin/
    foo.py that imports a script that at ~/bar/bin/bar.py. The imported
    file needs to load something from '~/bar/bin/data/', but I cannot hard
    code the full path. Instead, what I need is to have something in
    bar.py like

    dir = .... # Not sure what this line should be
    datadir = dir + '/data/'

    If I use sys.path[0] or os.getcwd(), then when I import the file from
    ~/foo/bin/, I get that dir is '~/foo/bin' rather than '~/bar/bin'

    I hope this explanation makes sense. If you have any insights, I would
    be most appreciative.

    Thanks
    Are
    are Dogue, May 18, 2011
    #1
    1. Advertising

  2. are Dogue

    are Dogue Guest

    Nevermind...
    os.path.dirname (__file__)


    On May 17, 9:44 pm, are Dogue <> wrote:
    > Hi there,
    >
    > I have a problem that I can't seem to solve after quite a bit of
    > searching, probably because I can't find the right terms to search
    > for.
    >
    > Basically, here's the situation. Let's say I have a file at ~/foo/bin/
    > foo.py that imports a script that at ~/bar/bin/bar.py. The imported
    > file needs to load something from '~/bar/bin/data/', but I cannot hard
    > code the full path. Instead, what I need is to have something in
    > bar.py like
    >
    > dir = .... # Not sure what this line should be
    > datadir = dir + '/data/'
    >
    > If I use sys.path[0] or os.getcwd(), then when I import the file from
    > ~/foo/bin/, I get that dir is '~/foo/bin' rather than '~/bar/bin'
    >
    > I hope this explanation makes sense. If you have any insights, I would
    > be most appreciative.
    >
    > Thanks
    > Are
    are Dogue, May 18, 2011
    #2
    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. RonS
    Replies:
    5
    Views:
    4,398
  2. Sigmathaar
    Replies:
    6
    Views:
    464
    Sigmathaar
    Dec 5, 2005
  3. ll
    Replies:
    3
    Views:
    971
  4. Jennifer
    Replies:
    1
    Views:
    154
    Tad McClellan
    Jun 24, 2003
  5. Replies:
    3
    Views:
    156
    Jürgen Exner
    May 12, 2009
Loading...

Share This Page