[Tutor] finding directory of self

Rance Hall ranceh at gmail.com
Tue Mar 29 01:43:19 CEST 2011


I had been doing this under windows:

import os
import sys

osname = os.name
pathtocfg = os.path.dirname(sys.argv[0])
configfileloc = os.path.abspath(pathtocfg)
os.chdir(configfileloc)

to set the directory of all subsequent file lookups in a script.

It worked underwindows because the shortcuts have a "Start In"
directory listing.

I need something for Linux use that does not care about a symlink that
would be used to start the script.

The above code finds the directory the symlink is in.

I'm actually not suprised by this, it is what I expected, I just don't
understand how to fix it.

How can I do something similar to this, but find the "real" dir?
Perhaps one of the other sys.argv arguments?

Thanks for your help

Rance


More information about the Tutor mailing list