read subprocess stdout line by line

后端 未结 9 2076
一个人的身影
一个人的身影 2020-11-22 02:15

My python script uses subprocess to call a linux utility that is very noisy. I want to store all of the output to a log file and show some of it to the user. I thought the

9条回答
  •  自闭症患者
    2020-11-22 02:58

    Indeed, if you sorted out the iterator then buffering could now be your problem. You could tell the python in the sub-process not to buffer its output.

    proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)
    

    becomes

    proc = subprocess.Popen(['python','-u', 'fake_utility.py'],stdout=subprocess.PIPE)
    

    I have needed this when calling python from within python.

提交回复
热议问题