Path ... where is my application's home dir?

Tim Golden tim.golden at viacom-outdoor.co.uk
Wed Apr 28 11:33:16 EDT 2004


>On Wed, 28 Apr 2004 14:44:30 +0100, Tim Golden 
><tim.golden at viacom-outdoor.co.uk> wrote:
>
>>> I want to store a file in the application directory. What is
>>> the easiest
>>> way to figure out, where my application is situated?
>>>
>>> sys.argv[0] does not work for my purposes... do you have any
>>> other ideas.
>>
>> a) Why doesn't argv[0] work?
>
>Because I am stupid and tested always the old/unchanged 
>version where the 
>sys.argv had no impact. 8o((((
>Thanks, your a) hint - it was the kick that I needed! The look over my 
>shoulder!
>
>But I still wonder: Suppose sys.argv[0] wouldn't work. What 
>would be the 
>easiest way to find out where an application lies?
>
>Ideas:
>1. going through sys.path and checking whether the application 
>file is in 
>a dir
>2. or is there a module method, that tells you where it is stored?

As far as I know, it's effectively impossible: the "application" you're
running is the Python executable, so even if you were do some fancy
stuff with the win32 process/thread API and work out who you were
and where you were running, that'd probably give you:
c:/pythonxx/python.exe.
It's down to the application (ie the python executable) to store its
command-line, and it does that in sys.argv[0].

Having said that, if you were desperate, you could probably fish
through the Windows process info to find the full command line
which spawned the thing you're running, and then work out from
its parameters whether or not the script (which would be the first
non-switch parameter, if any) was given an explicit path -- in which
case, there you are -- or not -- in which case, os.getcwd ().

But I doubt it's worth it.

TJG


________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________




More information about the Python-list mailing list