newbie question: how to get directory of script?

Robin Munn rmunn at pobox.com
Wed Nov 13 01:59:25 EST 2002


Tom McDermott <spon at cisco.com> wrote:
> djw wrote:
>> 
>> os.getcwd()
>> 
> No such luck. This (at least on linux) returns the directory from which
> the script is invoked, rather than the directory of the script.
> 
> I use sys.path[0], but that doesn't seem to be the official method.

I don't know why Windows is putting the name of the script in
sys.argv[1] instead of sys.argv[0] -- that bothers me. But if you know
the name of the script, then you can do:

script_dir = os.path.dirname(os.path.abspath(script_name))

Or:

script_dir, script_basename = os.path.split(os.path.abspath(script_name))

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838



More information about the Python-list mailing list