import os print __file__ print os.path.dirname(__file__) os.chdir('/tmp') print __file__
I have this question above where dirname(__file__) can no longer be relied upon after os.chdir was used in the script, after the module loader installed __file__ .
What is the usual mechanism for working on this if you do not know where / when / how os.chdir could have been called before?
edit: I hope this second example can better clarify my problem
import os old_dir = os.getcwd() print os.path.abspath(__file__) os.chdir('/tmp') print os.path.abspath(__file__) os.chdir(old_dir)
The output is as follows:
wim@wim-acer :~$ python --version Python 2.7.1+ wim@wim-acer :~$ pwd /home/wim wim@wim-acer :~$ python /home/wim/spam.py /home/wim/spam.py /home/wim/spam.py wim@wim-acer :~$ python ./spam.py /home/wim/spam.py /tmp/spam.py
source share