Help with paths

Discussion in 'Python' started by Devin M, Oct 18, 2010.

  1. Devin M

    Devin M Guest

    Hello, I am using os.path to get the absolute paths of a few
    directories that some python files are in.
    FIlePath = os.path.dirname(os.path.realpath(__file__))
    which returns a path similar to /home/devinm/project/files
    Now I want to get the directory above this one. (/home/devinm/
    project/) Is there a simple way to do this? I was considering spliting
    apart the path and then reconstructing it with the last folder left
    off. Hope theres a better way to do this.

    Regards,
    Devin M
     
    Devin M, Oct 18, 2010
    #1
    1. Advertising

  2. On Mon, 18 Oct 2010 14:24:39 -0700 (PDT) Devin M <>
    wrote:

    > Hello, I am using os.path to get the absolute paths of a few
    > directories that some python files are in.
    > FIlePath = os.path.dirname(os.path.realpath(__file__))
    > which returns a path similar to /home/devinm/project/files
    > Now I want to get the directory above this one. (/home/devinm/
    > project/) Is there a simple way to do this? I was considering spliting
    > apart the path and then reconstructing it with the last folder left
    > off. Hope theres a better way to do this.
    >

    Maybe os.relpath("..", FilePath) ? Python > 2.6 only.

    Don't know if that does what you want, but from the description, it
    seems to fit:

    <http://docs.python.org/library/os.path.html>

    os.path.relpath(path[, start])

    Return a relative filepath to path either from the current
    directory or from an optional start point.

    start defaults to os.curdir.


    /W

    --
    To reach me via email, replace INVALID with the country code of my home
    country. But if you spam me, I'll be one sour Kraut.
     
    Andreas Waldenburger, Oct 18, 2010
    #2
    1. Advertising

  3. On Mon, 18 Oct 2010 17:29:11 -0400 Andreas Waldenburger
    <> wrote:

    > On Mon, 18 Oct 2010 14:24:39 -0700 (PDT) Devin M <>
    > wrote:
    > [snip]
    >
    > Maybe os.relpath("..", FilePath) ? Python > 2.6 only.
    >

    Gah! I should learn to copy&paste more. Obviously it's the second one
    (os.path.relpath), not the first.

    /W

    --
    To reach me via email, replace INVALID with the country code of my home
    country. But if you spam me, I'll be one sour Kraut.
     
    Andreas Waldenburger, Oct 18, 2010
    #3
  4. Devin M

    Chris Rebert Guest

    On Mon, Oct 18, 2010 at 2:24 PM, Devin M <> wrote:
    > Hello, I am using os.path to get the absolute paths of a few
    > directories that some python files are in.
    > FIlePath = os.path.dirname(os.path.realpath(__file__))
    > which returns a path similar to /home/devinm/project/files
    > Now I want to get the directory above this one. (/home/devinm/
    > project/) Is there a simple way to do this?


    parent_dir = os.path.split(FilePath)[0]

    Note that os.split() does not work like str.split().
    Also, don't use CamelCase for non-classes.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Oct 18, 2010
    #4
  5. On 10/18/2010 2:24 PM Devin M said...
    > Hello, I am using os.path to get the absolute paths of a few
    > directories that some python files are in.
    > FIlePath = os.path.dirname(os.path.realpath(__file__))
    > which returns a path similar to /home/devinm/project/files
    > Now I want to get the directory above this one. (/home/devinm/
    > project/) Is there a simple way to do this? I was considering spliting
    > apart the path and then reconstructing it with the last folder left
    > off. Hope theres a better way to do this.


    Yes -- in the same module...

    >>> os.__file__

    'C:\\Python26\\lib\\os.pyc'
    >>> os.path.dirname(os.__file__)

    'C:\\Python26\\lib'
     
    Emile van Sebille, Oct 18, 2010
    #5
  6. On 18.10.2010 23:24, Devin M wrote:
    > Hello, I am using os.path to get the absolute paths of a few
    > directories that some python files are in.
    > FIlePath = os.path.dirname(os.path.realpath(__file__))
    > which returns a path similar to /home/devinm/project/files
    > Now I want to get the directory above this one. (/home/devinm/
    > project/) Is there a simple way to do this? I was considering spliting
    > apart the path and then reconstructing it with the last folder left
    > off. Hope theres a better way to do this.
    >
    > Regards,
    > Devin M


    os.path.split() is designed for this

    In [4]: path="/home/devinm/project/files"

    In [5]: import os.path

    In [6]: os.path.split(path)[0]
    Out[6]: '/home/devinm/project'
     
    Alexander Kapps, Oct 18, 2010
    #6
  7. Devin M

    Devin M Guest

    On Oct 18, 2:43 pm, Alexander Kapps <> wrote:
    > On 18.10.2010 23:24, Devin M wrote:
    >
    > > Hello, I am using os.path to get the absolute paths of a few
    > > directories that some python files are in.
    > > FIlePath = os.path.dirname(os.path.realpath(__file__))
    > > which returns a path similar to /home/devinm/project/files
    > > Now I want to get the directory above this one. (/home/devinm/
    > > project/) Is there a simple way to do this? I was considering spliting
    > > apart the path and then reconstructing it with the last folder left
    > > off. Hope theres a better way to do this.

    >
    > > Regards,
    > > Devin M

    >
    > os.path.split() is designed for this
    >
    > In [4]: path="/home/devinm/project/files"
    >
    > In [5]: import os.path
    >
    > In [6]: os.path.split(path)[0]
    > Out[6]: '/home/devinm/project'


    Thank you for the help
     
    Devin M, Oct 19, 2010
    #7
    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. Noah
    Replies:
    5
    Views:
    814
  2. ksamdev
    Replies:
    27
    Views:
    1,018
    ksamdev
    Dec 15, 2008
  3. mark | r

    help hiding image paths

    mark | r, Jul 4, 2005, in forum: ASP General
    Replies:
    7
    Views:
    158
    Roland Hall
    Jul 8, 2005
  4. Ohad Lutzky

    Paths, gentleman, paths

    Ohad Lutzky, Nov 6, 2006, in forum: Ruby
    Replies:
    2
    Views:
    210
    David Vallner
    Nov 7, 2006
  5. typemismatch
    Replies:
    3
    Views:
    122
    typemismatch
    Jul 19, 2009
Loading...

Share This Page