Saving stdout from subprocess.Popen to file line by line

前端 未结 4 1823
滥情空心
滥情空心 2021-02-06 05:42

My python script uses subprocess to call an another script, which produces output very slow(line-by-line basis). I would like to write the output line by line to file not when t

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 05:52

    Yes, it is possible. Here is a function that I wrote for a test harness use to do unit testing of Python shell scripts.

    def testrun(cmdline):
       try:
          cmdout, cmderr = "",""
          cmdp = Popen(cmdline, shell=True,stdout=PIPE, stderr=PIPE)
          cmdout,cmderr =  cmdp.communicate()
          retcode = cmdp.wait()
          if retcode < 0:
             print >>sys.stderr, "Child was terminated by signal", -retcode
          else:
             return (retcode,cmdout,cmderr)
       except OSError, e:
          return (e,cmdout,cmderr)
    

    The function returns a tuple which contains the shell return code issues by sys.exit(), the standard output text, and the standard error output text. They are both text strings so you would need to use splitlines to break them into lines before processing.

    If you really need to interact with the output, line by line, then it is probably better to use pexpect rather than the subprocess module.

提交回复
热议问题