您可以链接命令以允许更改目录:
from os import system
system(
"cd ./assets/" "; "
"bash loadData.sh" ";"
# here should come tasks in the python-source
"ls -al >> ./../directory" "; "
)
但是,在完成python中的一些计算之后,如何执行第二个命令?/如何存储bash-context并在以后重用它?
修改
输出可以显示到stdout或子进程中的管道对象,但是bash-code中的目录更改和赋值变量应该以任何形式保存(我想要执行real bash-commands来更改目录,不想使用os.chdir
)
答案 0 :(得分:0)
我不确定你到底想做什么,但你甚至可以用子进程做非常复杂的变量赋值,如下所示:
import subprocess as sp
p = sp.Popen("cd assets; var=$(bash loadData.sh); var2=$(python -c \"print '${var}123'\"); echo ${var2}", stdout=sp.PIPE, shell=True)
output = p.stdout.read()
print output