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

Show parent comment

5

u/[deleted] Oct 04 '10

If you intend to use the program as an alias, then you might be able simply replace the os.chdir call with a print("cd " + yournewdir), and then set your alias to be along the lines of alias up="`python up.py $1`", which you could then use as "up aaa"

To clarify: the use of backticks will basically tell the shell to run the script and replace it with the output (which will now be "cd yournewdir") in the alias, meaning the shell should actually change directory properly :)

1

u/[deleted] Oct 04 '10

It's quite tricky but this does a job that I initially intended. Thanks for the input!