Vince
Vince

Reputation: 4401

how to change user's shell current directory using python ? (ubuntu/linux)

We can change the current directory in the shell :

username@hostname:~/f1$ cd ~/f2
username@hostname:~/f2$ 

is it possible to write a python (v2.7 and / or v3) which changes the current directory to one programatically determined ?

E.g.

username@hostname:~/f1$ python change_dir.py
username@hostname:~/f2$ 

Upvotes: 1

Views: 64

Answers (1)

Armali
Armali

Reputation: 19375

As chepner and Sraw correctly said, a child process cannot change the current directory of its parent. So, what you can do is to let the Python program output the desired directory and use this output in a cd command:

cd `python print_dir.py`

Upvotes: 1

Related Questions