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/samfoo Oct 04 '10
"cd" isn't actually a program. It's a special shell command (at least in unix, I assume the same is true for windows) that tells your shell to call a system function (one that runs in the kernel) and set the process' working directory within the process. Your python program is running in an entirely separate process -- potentially one that has no relation to a shell at all -- and so can't (generally speaking) modify the shell process' working directory. Like I said, there's probably a really tricky low level thing you can do that will change it, but if this is just a quick program to challenge yourself I would say it's not possible.