Name of the Script

David Bolen db3l at
Wed Mar 7 20:09:39 CET 2001

Paul Moore <paul.moore at> writes:

> OK, so you could say "don't do that". But if I'm looking at some code
> deep in a big program, can I be *sure* that no-one has changed
> directory on me?

You can't, but then again you really shouldn't be looking at your
startup arguments (IMHO) inside code deep in a big program.  That's
the sort of thing that should be cleanly isolated to startup
processing, although some of the processing can be deferred to other
functions if necessary (e.g, sort of the way the X library did
pre-processing on its known arguments in advance of local code).

> Interestingly, although the __name__ symbol has the value __main__ in
> a script being run as a "main program", the __file__ symbol doesn't
> exist. That's a pity, as it would probably be what I wanted...

Right - if you're being imported you can reference __file__, but a top
level script really only has argv[0], I believe.

But argv[0] always references the path used to start the script which,
at the point of startup, should always be a path that you can use to
reference the same location.

I'd just suggest that if you need this sort of thing later that you
identify your location at startup, and then make that information
available in a standard way to the rest of the application independent
of sys.argv.  And if you know you might be moving directories, store
the os.path.abspath() of sys.argv[0] at startup rather than just

-- David
 \               David Bolen            \   E-mail: db3l at  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \

More information about the Python-list mailing list