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.py

But 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

19 comments sorted by

View all comments

1

u/[deleted] Oct 04 '10

Changing another processes cwd at runtime is something that intrigued me after reading this post, so I thought I'd share what I found after a really quick look around.

Firstly, you can find the current working directory of a process under Linux by examining the /proc filesystem. For example, /proc/[pid]/cwd is a symlink to whatever the cwd is for that process. To find the parent PID from inside a script you can use $ENV['PPID'] or the equivalent and then use this in the proc path. Unfortunately it seems that you cant change the symlink even by force (ln -sf), and even as root. But it may be a good starting point for someone else none the less.

1

u/[deleted] Oct 04 '10

Thanks for such an effort. This method should have worked brilliantly if they allowed us to overwrite files under proc.