Store output of subprocess.Popen call in a string

前端 未结 15 2234
一个人的身影
一个人的身影 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:52

    Assuming that pwd is just an example, this is how you can do it:

    import subprocess
    
    p = subprocess.Popen("pwd", stdout=subprocess.PIPE)
    result = p.communicate()[0]
    print result
    

    See the subprocess documentation for another example and more information.

提交回复
热议问题