[Tutor] how to expand "../../myfile"

Glen Jarvis glen at glenjarvis.com
Sat Jul 19 06:42:23 CEST 2014


On Fri, Jul 18, 2014 at 6:33 PM, Alex Kleider <akleider at sonic.net> wrote:

> Is there a function that can return the full path name when provided with
> something along the lines of
> "../../myfile"?
>

The OS module is great at this. Here's an example:

import os

MYFILE= '../../myfile'

print "MYFILE", MYFILE

print "abspath", os.path.abspath(MYFILE)


And, here is the output:

prompt> python example01.py

MYFILE ../../myfile

abspath /Users/glenjarvis/Desktop/test/myfile


>
> Just to be able to glean the current working directory would suffice. (I
> could use a shell command if I had to but was hoping it could be done all
> within Python.
>

You can also glean just the current working directory:

import os

print "pwd", os.getcwd()


And it's output:

prompt> python example02.py

pwd /Users/glenjarvis/Desktop/test/x/y


Cheers,


Glen
-- 

"You grab mindshare by being there."

-- Alex Martelli

   Bay Area Python Interest Group Talk

   24-Oct, 2013
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140718/63f72a7a/attachment.html>


More information about the Tutor mailing list