Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help with paths

Reply
Thread Tools

Help with paths

 
 
Devin M
Guest
Posts: n/a
 
      10-18-2010
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
 
Reply With Quote
 
 
 
 
Andreas Waldenburger
Guest
Posts: n/a
 
      10-18-2010
On Mon, 18 Oct 2010 14:24:39 -0700 (PDT) Devin M <(E-Mail Removed)>
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.

 
Reply With Quote
 
 
 
 
Andreas Waldenburger
Guest
Posts: n/a
 
      10-18-2010
On Mon, 18 Oct 2010 17:29:11 -0400 Andreas Waldenburger
<(E-Mail Removed)> wrote:

> On Mon, 18 Oct 2010 14:24:39 -0700 (PDT) Devin M <(E-Mail Removed)>
> 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.

 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      10-18-2010
On Mon, Oct 18, 2010 at 2:24 PM, Devin M <(E-Mail Removed)> 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
 
Reply With Quote
 
Emile van Sebille
Guest
Posts: n/a
 
      10-18-2010
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'


 
Reply With Quote
 
Alexander Kapps
Guest
Posts: n/a
 
      10-18-2010
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'
 
Reply With Quote
 
Devin M
Guest
Posts: n/a
 
      10-18-2010
On Oct 18, 2:43*pm, Alexander Kapps <(E-Mail Removed)> 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
 
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
Help with relative paths using require typemismatch Ruby 3 07-19-2009 08:08 PM
HELP: BASE and LOCAL paths at the same time? ksamdev HTML 27 12-15-2008 09:36 PM
Paths, gentleman, paths Ohad Lutzky Ruby 2 11-07-2006 02:15 AM
help hiding image paths mark | r ASP General 7 07-08-2005 12:59 AM
Convert between Windows style paths and POSIX style paths Noah Python 5 07-11-2003 09:25 PM



Advertisments