Is there a quiet version of subprocess.call?

后端 未结 4 1648
渐次进展
渐次进展 2020-12-10 10:27

Is there a variant of subprocess.call that can run the command without printing to standard out, or a way to block out it\'s standard out messages?

4条回答
  •  情话喂你
    2020-12-10 10:38

    Often that kind of chatter is coming on stderr, so you may want to silence that too. Since Python 3.3, subprocess.call has this feature directly:

    To suppress stdout or stderr, supply a value of DEVNULL.

    Usage:

    import subprocess
    rc = subprocess.call(args, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
    

    If you are still on Python 2:

    import os, subprocess
    
    with open(os.devnull, 'wb') as shutup:
        rc = subprocess.call(args, stdout=shutup, stderr=shutup)
    

提交回复
热议问题