How to read the first byte of a subprocess's stdout and then discard the rest in Python?

后端 未结 2 1103
小鲜肉
小鲜肉 2020-12-05 06:43

I\'d like to read the first byte of a subprocess\' stdout to know that it has started running. After that I\'d like to discard all further output, so that I don\'t have to w

2条回答
  •  渐次进展
    2020-12-05 07:31

    If you're using Python 3.3+, you can use the DEVNULL special value for stdout and stderr to discard subprocess output.

    from subprocess import Popen, DEVNULL
    
    process = Popen(["mycmd", "myarg"], stdout=DEVNULL, stderr=DEVNULL)
    

    Or if you're using Python 2.4+, you can simulate this with:

    import os
    from subprocess import Popen
    
    DEVNULL = open(os.devnull, 'wb')
    process = Popen(["mycmd", "myarg"], stdout=DEVNULL, stderr=DEVNULL)
    

    However this doesn't give you the opportunity to read the first byte of stdout.

提交回复
热议问题