Wrap subprocess' stdout/stderr

后端 未结 4 2011
既然无缘
既然无缘 2020-12-05 16:00

I\'d like to both capture and display the output of a process that I invoke through Python\'s subprocess.

I thought I could just pass my file-like object as named pa

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 17:06

    Is there a reason you have a class inside a class? And stdout and stderr can take any file like line for example try. So simply passing an open file type or stringIO should be enough to modify the stream

    import sys
    sys.stdout = open('test.txt','w')
    print "Testing!"
    sys.stdout.write('\nhehehe')
    sys.stdout = sys.__stdout__
    sys.exit(0)
    

提交回复
热议问题