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?
Eg
username@hostname:~/f1$ python change_dir.py
username@hostname:~/f2$
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`
Maybe you mean something like this:
import os
os.chdir(os.path.join(os.path.dirname(os.getcwd()), 'f2'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.