[英]Suppress output from subprocess.Popen
如何從 subprocess.Popen 中阻止 output 成為 output? 如果打印量很大,打印有時會很慢。
如果你想完全扔掉它:
import subprocess
import os
with open(os.devnull, 'w') as fp:
cmd = subprocess.Popen(("[command]",), stdout=fp)
如果您使用的是 Python 2.5,則需要from __future__ import with_statement
,或者不要使用with
。
在 Python 3.3+ 中,您可以使用subprocess.DEVNULL
來抑制 output:
from subprocess import DEVNULL, STDOUT, check_call
check_call([cmd, arg1, arg2], stdout=DEVNULL, stderr=STDOUT)
如果您也不想抑制stderr
,請刪除stderr=STDOUT
。
這也對我有用(Python 3.6、Ubuntu Linux 操作系統):
subprocess.Popen(cmd, shell=False, stdout=DEVNULL)
- 這假設您希望 cmd 在控制台中進行非阻塞呼叫並且沒有垃圾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.