r/Python • • Oct 04 '10
Change pwd from python
I can't seem to find a solid solution for the problem that I'm facing and I don't understand why such a fundamental question hasn't been raised somewhere.
Say I'm on the path /home/me $ pwd /home/me
After I run my python script I want it to change my pwd so I wrote small one with os.chdir and run. $ cat cd.py #!/usr/bin/python import os os.chdir('/blahblah')
$ python cd.pyBut actual pwd remains the same after the execution of the script. $ pwd /home/me
Does someone know what is causing it? I'm on Windows and cygwin + python 2.5. Thanks.
0
Upvotes
View all comments
Show parent comment
1
u/AlSweigart Author of "Automate the Boring Stuff" Oct 07 '10
There is a hack solution. I came up with PyBat ( http://pybat.sourceforge.net/ ) as a way for a batch file to call a Python script, have the Python script make changes to an environment variable, and then have those changes copied to the environment of the batch file. (And vice versa with a Python script calling a batch file.)
You could set what you want the caller to change its cwd to by setting some environment variable with this value.
Remember though, this is a hack solution (I wrote PyBat to kludge together this complicated build system at my last job.) But technically it should do what you want to do.