Best way to find starting directory

Frank Millman frank at chagford.com
Tue Mar 19 03:26:36 EDT 2013


Hi all

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()

2.
import os.path
import __main__
dir = os.path.dirname(__main__.__file__)

I know that the first one will return the wrong result if os.chdir() has 
been executed, but I don't do that.

Apart from that, is either version preferred over the other, or is there 
another way?

Thanks

Frank Millman




More information about the Python-list mailing list