如何在bash中调用python并更改工作目录?
我有一个bash脚本,内容如下:
# gets all relevant files in the directory
cp ../update_files/* ./transfer_dir
# copy the python scripts to that directory
cp ../tools/update_tool/* ./transfer_dir
# execute the python scripts
python ./transfer_dir/merge.py
现在问题是,当我尝试执行这个python脚本时,发现“工作目录”是.,而不是./transfer_dir,这样我就找不到之前复制的update_files了。
我该怎么改变这个呢?我不想对我的python脚本做太多修改,因为它们大多数情况下和位置无关。
2 个回答
1
你可以在bash脚本中更改路径。
可以参考Ubuntu的回答,了解如何通过bash更改工作目录,链接在这里:change working dir via bash你也可以在脚本里直接更改工作目录。
比如,你可以这样写:
import os
os.chdir("transfer_dir")
我建议使用第一种方法,第二种只是为了完整性而提到的。
7
使用 cd
命令:
cd transfer_dir
# execute the python scripts
python merge.py
# restore old directory
cd ..