Best way to find starting directory

Dave Angel davea at davea.name
Tue Mar 19 13:46:45 CET 2013


On 03/19/2013 04:21 AM, Frank Millman wrote:
> On 19/03/2013 09:55, Peter Otten wrote:
>> Frank Millman wrote:
>>
>>> I want to locate a file relative to the directory from which the main
>>> program was launched.
>>>
>>> I have found two ways of finding the starting directory -
>>>
>>> 1.
>>> import os
>>> dir = os.getcwd()
>>
>> This gives the current working directory...
>>
>>> 2.
>>> import os.path
>>> import __main__
>>> dir = os.path.dirname(__main__.__file__)
>>
>> ... and this gives the location of your main script.
>>
>>> I know that the first one will return the wrong result if os.chdir() has
>>> been executed, but I don't do that.
>>
>> You'll get different results when you launch the script with an explicit
>> path:
>>
>> $ cat millman/demo.py
>> import os
>> import __main__
>>
>> print "cwd:", os.getcwd()
>> print "script path:", os.path.abspath(os.path.dirname(__main__.__file__))
>> $ python millman/demo.py
>> cwd: /home/frank
>> script path: /home/frank/millman
>>
>>
>
> That makes sense. I usually launch the script from its own directory,
> but that is not guaranteed.
>
> Therefore option 2 is the way to go.

You might want to reconsider.  There are really two different kinds of 
data files you might want to access from your script. The first is 
constant data that gets initialized when the script is installed.  And 
the second is user data that he's thinking about right now.

For example, if a script uses a saved cache of prime numbers to make 
calculations a bit faster, it might keep that file in with its own 
source code, or relative to it.

And if I wanted to calculate md5 sums for a directory tree, I'd usually 
make that my cwd before starting the script.

Config files are somewhere in between.  In Linux, get them relative to 
the $HOME environment variable.

FWIW, I try to keep all the first kind of files on a separate partition, 
and except where other programs force me, never let them leak onto the 
OS+program partition.  That way, I'm not likely to lose an important 
jpeg when the OS trashes its partition.  This particular paranoia is 
left over from my Windows days, but I stick to it anyway.  It also makes 
it easier to migrate to a new OS.  Just format the OS partition and 
install the OS and all the apps.  The data is already separate.

-- 
DaveA



More information about the Python-list mailing list