[英]how to change user's shell current directory using python ? (ubuntu/linux)
We can change the current directory in the shell:我们可以改变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?是否可以编写一个 python(v2.7 和/或 v3),将当前目录更改为以编程方式确定的目录?
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.正如 chepner 和 Sraw 正确所说,子进程不能更改其父进程的当前目录。 So, what you can do is to let the Python program output the desired directory and use this output in a
cd
command:所以,你可以做的是让 Python 程序 output 所需的目录,并在
cd
命令中使用这个 output:
cd `python print_dir.py`
Maybe you mean something like this:也许你的意思是这样的:
import os
os.chdir(os.path.join(os.path.dirname(os.getcwd()), 'f2'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.