Store output of subprocess.Popen call in a string

前端 未结 15 2508
一个人的身影
一个人的身影 2020-11-22 03:23

I\'m trying to make a system call in Python and store the output to a string that I can manipulate in the Python program.

#!/usr/bin/python
import subprocess         


        
15条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 03:51

    This worked for me for redirecting stdout (stderr can be handled similarly):

    from subprocess import Popen, PIPE
    pipe = Popen(path, stdout=PIPE)
    text = pipe.communicate()[0]
    

    If it doesn't work for you, please specify exactly the problem you're having.

提交回复
热议问题