[Tutor] Current path (of program)

Adrian Greyling adrian.greyling at gmail.com
Thu Sep 25 21:58:35 CEST 2008


Is it a faux pas to answer your own question??  I found this after I tweaked
my search terms..

>From "Dive Into Python" (
http://diveintopython.org/functional_programming/finding_the_path.html)<http://diveintopython.org/functional_programming/finding_the_path.html>

pathname = os.path.dirname(sys.argv[0])
currpath = os.path.abspath(pathname)

"Regardless of how you run a script, sys.argv[0] will always contain the
name of the script, exactly as it appears on the command line.  This may or
may not include any path information.  os.path.dirname takes a filename as a
string and returns the directory path portion. If the given filename does
not include any path information, os.path.dirname returns an empty string.
 os.path.abspath is the key here. It takes a pathname, which can be partial
or even blank, and returns a fully qualified pathname."


Sorry about that folks...
Adrian







On Thu, Sep 25, 2008 at 3:25 PM, Adrian Greyling
<adrian.greyling at gmail.com>wrote:

> I've been using "os.getcwd()" to get my program's "current path".  I know
> it's actually returning my "current working directory", but it's been
> working okay, until today...  I used py2exe (and InnoSetup) to create a
> standalone executable (for Windows) and then a shortcut icon on the desktop
> to "MyProg.exe".  Problem is, now "os.getcwd()" returns "C:\Documents and
> Settings\ME\Desktop\" as it's current working directory, not the "C:\Program
> Files\MyCoolProgram\" that I was expecting.
>
> I'm trying to use a "relative path reference" so that the user can install
> the program to whatever directory he/she wants, but that my program won't
> "lose track" of the subdirectories it requires for additional files.
>
> Anyone run into the same sort of problem?  Better yet, anyone know how to
> solve this?
>
> Thanks everyone!
> Adrian
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080925/1b3354a8/attachment.htm>


More information about the Tutor mailing list