os.system() を使用して Python プログラムを実行し、その出力をファイルに記録しようとしています。これはうまくいきます。
os.system("myprogram.py -arg1 -arg2 > outputfile.txt")
#something here to kill/cleanup whatever is left from os.system()
#read outputfile1.txt- this output file got all the data I needed from os.system()
問題は、myprogram.py が別の python プログラムを呼び出して、必要な出力が得られるが終了しないことです。下の図のように、プロンプトが異なることもわかります。
os.system("quit()") と subprocess.popen("quit()", shell=False) を使用して試したプログラムの次の行に到達したときに子プロセスを強制終了する方法はありますか何もしないでください。
exit() は実際には使用できません。これは、python をまとめて殺すだけだからです。
ところで、これは失速します
f=subprocess.Popen("myprogram.py -arg1 > outputfile.txt") and then
f.communicate() #this just stalls because the child program does not end.