views:

739

answers:

2

How do i chdir to the directory that the python script is in? so far i figured out os.chdir and sys.argv[0]. I'm sure there is a better way then to write my own func to parse argv[0]

+6  A: 
os.chdir(os.path.dirname(__file__))
ayrnieu
for whatever reason __file__ was C:\dev\Python25\Lib\idlelib so a quick replace with argv[0] solved it. +1 and check marked
acidzombie24
Also, depending on platform you may want to use `os.path.abspath` on the result of `os.path.dirname` to make sure any symbolic links or other filesystem redirection get expanded properly.
James Bennett
+3  A: 

Sometimes __file__ is not defined, in this case you can try sys.path[0]