私はwhileループpythonスクリプトを実行して出力をファイルに書き込む方法は?
from time import sleep
while True:
print "hola"
print "mundo"
sleep(2)
とPythonで次のスクリプトを持っていると私は、次のコードで出力をファイルに書きたい:
import subprocess
with open("output.log", "w") as output:
subprocess.call(["python", "./main.py"], stdout=output);
事はしばらくは決して終わらないということですファイルoutput.logは決してスクリプトからの出力を得ません。私はそれを行う方法があるのだろうかと思います。
['import logging'](https://docs.python.org/2.6/library/logging.html#simple-examples) – JRodDynamite
pythonのstdoutはデフォルトでバッファリングされます。サブプロセスとしてpythonを呼び出すときに 'print'の後に' sys.stdout.flush() 'を追加するか、' -u'オプションを追加してください。 – ymonad
sys.stdout.flush()は動作しますが、 ? –