Best way to find starting directory

Frank Millman frank at
Tue Mar 19 08:26:36 CET 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 -

import os
dir = os.getcwd()

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?


Frank Millman

More information about the Python-list mailing list