Help with paths

D

Devin M

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
 
A

Andreas Waldenburger

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
 
A

Andreas Waldenburger

[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
 
C

Chris Rebert

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
 
E

Emile van Sebille

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...
'C:\\Python26\\lib'
 
A

Alexander Kapps

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'
 
D

Devin M

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top