1
我對執行python命令和shell命令(來自子進程)的順序感到困惑。按順序執行python命令和shell命令(使用子進程)
例如,我有一個簡單的代碼:
import subprocess
import shlex
command="echo 'test'"
arg=shlex.split(command)
with open("out.txt", "w") as f:
f.write("line1\n")
subprocess.call(arg, stdout=f)
f.write("line3\n")
我希望out.txt是:
line1
test
line3
然而,實際out.txt是:
test
line1
line3
有人可以解釋原因嗎? 謝謝。
謝謝你,湯姆。這真的很有幫助。我將檢查file.flush()的東西。 – dbrg77