Finding Script Directory

Peter Hansen peter at engcorp.com
Wed Jul 7 17:07:37 CEST 2004


Steve Holden wrote:

> Peter Hansen wrote:
>> Fuzzyman wrote:
>>> What's the best, cross platform, way of finding out the directory a
>>> script is run from ?
>>>
>>> I've googled a bit, but can't get a clear answer.
>>
>> I've seen twenty threads on this and I still don't know/recall
>> whether there's a clear answer. :-(
> Oh, come now, Peter, you've seen this problem solved more times than you 
> can shake a stick at. The required code is
> 
>     import os.path, sys
>     print os.path.abspath(sys.argv[0])
> 
> How hard can that be to remember? Didn't realise you were getting so OLD 
>  ;-) ...

Well, I just put that in a nice little .exe created by py2exe,
and running from a directory in my PATH.

Amazing, but when I run it with the current directory set to C:\
it prints only "C:\testpath", and yet the script (well, the EXE)
is actually in a directory called c:\dev\prj189~1\dist ...

Fuzzyman's right: there isn't a clear, conclusive answer, I believe.

(But the above recipe, which I'm sure he found via google,
should work in almost all cases, py2exe and perhaps certain OSes
excluded.)

-but-that-doesn't-mean-i'm-not-getting-old-ly y'rs,
  Peter



More information about the Python-list mailing list