简体   繁体   中英

Python subprocess.PIPE redirect to file

I have the following piece of code. myprg has to run after my script exits. But before exiting, I would like to wait for mystring to be emitted by myprg and then redirect rest of the stderr messages to mylogfile and then exit. Is this possible at all? I tried the following and the python script does not exit. Where am I going wrong?

import subprocess
proc = subprocess.Popen(myprg, stderr=subprocess.PIPE)

for line in iter(proc.stderr.readline,'')
if check_string in line:
    break

fh = open (mylogfile,'w')

proc.stderr = fh.fileno()

EDIT: I have temporarily worked around this problem by making myprg to emit check_string to stdout. This is less than optimal, but works for the moment.

您不应该调用proc.communicate()退出脚本吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM