Skip to main content
 首页 » 编程设计

python之从 bash 调用 python,如何更改 python 工作目录

2025年05月04日14mate10pro

我有一个像这样的 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 脚本,因为它们大多与位置无关。

请您参考如下方法:

使用cd:

cd transfer_dir 
# execute the python scripts  
python merge.py 
# restore old directory 
cd ..