我喜欢在 python 或 bash 中执行类似的操作,其中程序会转换给定的文件路径并移动当前的 shell。
ulka:~/scc/utils$ python prog.py some_path1
ulka:some_path2$
这里
some_path1 -> prog.py -> some_path2
我尝试使用 subprocess.call 或 os.chdir,但它不起作用,任何想法都会受到赞赏。
最佳答案
由于 python 在自己的进程中运行,因此它无法更改 shell 的当前目录。但是,您可以这样做:
change_path() {
# prog.py figures out the real path that you want and prints
# it to standard output
local new_path=$(python prog.py some_path1) # could use an argument "$1"
cd "$new_path"
}
关于python - 从 python 或 shell 脚本更改工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42937563/