cd to dir after exiting script (system independent way, purely in python)

python, shell

Solution

But I want to be in the / dir after exiting the python interpreter, is that possible using python?

It is not possible. Neither by using Python or any other "acceptable" way. By acceptable, I mean "without outrageously hacking your system (with gdb for example)" ;)

More seriously, when the user launch an executable from a shell, the child process will run in its own environment, which is mostly a copy of its parent environment. This environment contains "environment variables" as well as the "current working directory", just to name those two.

Of course, a process can alter its environment. For example to change its working directory (like when you `cd xxx` in you shell). But since this environment is a copy this does not alter the parent's environment in any way. And there is no standard way to access your parent environment.

Problem

Observe the following: ``` $ pwd /home/username $ python >>> import os >>> os.chdir("/") # Ctrl + D $ pwd /home/username ``` But I want to be in the `/` dir after exiting the python interpreter, is that possible using python? I would like to know because I want to make a platform independent script(using python) where an optional convenience command `cd`'s the user into a certain directory.

Original source

Related problems