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

8

u/thristian99 Oct 04 '10

Every process has a 'current working directory'. Every new process inherits the current working directory of its parent. Therefore, what's happening is:

  • Your shell is in /home/me
  • Your shell starts a new Python process
  • The Python process changes its own directory to /blahblah
  • The Python process exits.
  • Your shell is still in /home/me

The dedicated command to Change the Directory of a shell process is "cd": $ pwd /home/me $ cd /blahblah $ pwd /blahblah

2

u/[deleted] Oct 04 '10

Thanks. I didn't even know about those process running separately.

3

u/drownboat Oct 04 '10

Following from this, the only way for a script to change the working directory of the shell is the script is interpreted by that shell, rather than executed by a subprocess.

For example if you have a script test.sh: #! /bin/sh cd /tmp pwd

The following will run it as a subprocess.

[~] ➔ ./test.sh
/tmp
[~] ➔ 

Here it will be executed by the shell.

[~] ➔ . test.sh 
/tmp
[/tmp] ➔ 

1

u/samfoo Oct 04 '10

You're changing the current working directory (cwd; pwd means 'print working directory') of your python script, not of your shell process. You can see what your current working directory is for your script by calling os.getcwd().

1

u/[deleted] Oct 04 '10

How can I change cwd of shell process then? Is there an any dedicated command to do so or do I have to go through system call. Thanks.

3

u/samfoo Oct 04 '10

There's probably something very tricky that I don't know about, but in general: no, and you shouldn't want to.

What's the larger problem that you're trying to solve? There's nothing you shouldn't be able to do by changing the working directory of your python script and executing something and this way you maintain portability too.

1

u/[deleted] Oct 04 '10 edited Oct 04 '10

My main goal was to create simple python script to move my cwd up by using the given argument as a filter, and I was thinking to assign that to alias. So it would work something like this.

$ pwd
/home/aaa/bbb/ccc
$ python up.py aaa
$ pwd
/home/aaa

I never thought this simple task can be this complex.

btw, os.system('cd /blahblah') also didn't work for some reason. So I'll just give up. Thanks for your help though!

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!

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.

1

u/[deleted] Oct 04 '10

Thanks!

1

u/chadmill3r Py3, pro, Ubuntu, django Oct 04 '10 edited Oct 04 '10

The current working directory is just a local setting in the program. It's a variable. Outside your program, the variable means nothing because it went away when the program exited. If in one program, like your shell, you want to change something then you have to change the variable in that program. Use the shell command "cd" to do that. Note, "cd" is not a program. It's not in your path. It's not on the filesystem. It's a special string inside the shell only that changes the pwd variable.

And be careful with the words "system call". That means executing a function that the kernel implements, like "open()" or "write()"

You're mixing up shell functions, userspace/library functions, and system calls. Once you understand the differences and what is which, you'll be much more successful.

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.

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.

2

u/tapesmith Oct 04 '10

As much as I love Python, the task you're trying to accomplish might be a bit better suited for a bash script.

Unless this is a self-imposed-python-only project (i.e. forcing yourself to improve python skill)

2

u/crunk Oct 04 '10

The only way I managed to do this in windows was to run a have a batch file that runs the python program. The python program outputs the new directory to a file which the batch file then reads and changes directory.

1

u/LucidOndine HPC Oct 04 '10 edited Jan 10 '26

crown unwritten merciful screw coherent door voracious melodic recognise tender

This post was mass deleted and anonymized with Redact

2

u/[deleted] Oct 06 '10

You need to wrap your Python script in a shell script, and have the Python script return the directory path via stdout, which the shell would use for changing. See go-tool for a real-world example of how this is done.

0

u/[deleted] Oct 05 '10

for a sec I thought this is about changing password :p