Best way to find starting directory

F

Frank Millman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top