[英]dump files from bash script in different directory from where python script ran it
我有一個python腳本與以下代碼。
Python script: /path/to/pythonfile/
Executable: /path/to/executable/
Desired Output Path: /path/to/output/
我的第一個猜測......
import subprocess
exec = "/path/to/executable/executable"
cdwrite = "cd /path/to/output/"
subprocess.call([cdwrite], shell=True)
subprocess.call([exec], shell=True)
這會將/path/to/pythonfile/
所有文件轉儲/path/to/pythonfile/
...我的意思是這是有意義的,但我不確定要假設什么'自我' - 我的python代碼看到的或shell腳本的內容,我以為它是在 shell中運行所以如果我在shell中cd,它將cd到所需的目錄並將輸出轉儲到那里?
發生的事情是兩個命令正在彼此獨立地執行。 你想要做的是進入目錄,然后執行。
subprocess.call(';'.join([cdwrite, exec]), shell=True)
您是否在與python文件相同的目錄中運行腳本? 使用現在的文件,文件應該輸出到您運行python腳本的目錄(可能是也可能不是腳本的目錄)。 這也意味着如果你給cd
的路徑是相對的,它將相對於你運行python腳本的目錄。
您應該在同一命令中更改目錄:
cmd = "/path/to/executable/executable"
outputdir = "/path/to/output/"
subprocess.call("cd {} && {}".format(outputdir, cmd), shell=True)
你可以使用cwd
參數:
from subprocess import check_call
cmd = "/path/to/executable/executable"
check_call([cmd], cwd="/path/to/output")
注意:不要不必使用shell=True
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.