Suppress output from subprocess.Popen

后端 未结 2 493
情话喂你
情话喂你 2020-12-06 00:06

How do you stop the output from subprocess.Popen from being output? Printing can sometimes be slow if there is a great deal of it.

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 00:50

    If you want to totally throw it away:

    import subprocess
    import os
    with open(os.devnull, 'w') as fp:
        cmd = subprocess.Popen(("[command]",), stdout=fp)
    

    If you are using Python 2.5, you will need from __future__ import with_statement, or just don't use with.

提交回复
热议问题