python, subprocess: reading output from subprocess

前端 未结 6 1418
暗喜
暗喜 2020-12-09 18:57

I have following script:

#!/usr/bin/python

while True:
    x = raw_input()
    print x[::-1]

I am calling it from ipython:

6条回答
  •  长情又很酷
    2020-12-09 19:07

    The subprocess method check_output can be useful for this:

    output = subprocess.check_output('./script.py')

    And output will be the stdout from the process. If you need stderr, too:

    output = subprocess.check_output('./script.py', stderr=subprocess.STDOUT)

    Because you avoid managing pipes directly, it may circumvent your issue.

提交回复
热议问题