[Tutor] Getting the directory the program is in

Jordan Greenberg greenbergj at wit.edu
Mon Dec 11 18:24:42 CET 2006


Tor Hildrum wrote:
> The full pathname is in sys.argv[0]
At least on my system, it only includes the filename if executed from
the current directory.

Example:

jordan at jordan-laptop:/$ cd ~
jordan at jordan-laptop:~$ cat > test.py
from sys import argv
print argv[0]
jordan at jordan-laptop:~$ python test.py
test.py
jordan at jordan-laptop:~$ cd /
jordan at jordan-laptop:/$ python test.py
/home/jordan/test.py
jordan at jordan-laptop:/$

So, if you _ALWAYS_ need an absolute path, just using sys.argv[0] might
not work.

Jordan Greenberg




More information about the Tutor mailing list