Find directory name of file?

Blair P. Houghton blair.houghton at
Mon Jan 30 15:25:31 EST 2006

Grant Edwards wrote:

> Try something like this at the beginning of your program and
> see if it does what you want:
>   print os.path.abspath(sys.argv[0])

Wanna see something freaky?

In IDLE, I type the following:

    >>> import sys
    >>> import os.path
    >>> os.path.abspath(sys.argv[0])
    'C:\\Program Files\\Python\\2.4'

Pretty normal, right?  Then I type this:

    >>> print sys.argv[0]


Yup.  No output.  Even if I wrap the sys.argv[0] in something visible,
there's nothing there:

    >>> print "x"+str(sys.argv[0])+"y"

How does it know what the path of the argument is if it doesn't know
what the argument is?  Seems a bit presumptive to assume that CWD is
the path to a blank string...unless abspath presumes that anything you
pass it is an executable, even if it's got no bytes...


More information about the Python-list mailing list