【发布时间】:2021-12-09 09:38:35
【问题描述】:
我正在尝试编写一个提取 wifi 密码的脚本。问题是,我不希望它在完全完成之前显示输出。
from subprocess import check_output
raw = check_output('security find-generic-password -ga test > /dev/null 2>&1',\
shell=True)
如果我像这样抑制输出,那么即使变量也不会得到任何输出以供进一步使用。那么如何抑制输出,但将其保存在变量中。
【问题讨论】:
-
不要将输出重定向到
/dev/null。check_output()的默认值是保存输出。 -
然后它也在终端中显示输出,这是我试图避免的。
-
不,不会的。
check_output()将标准输出重定向到管道并捕获管道的内容。 -
如果它产生错误并且您想捕获它,请使用
stderr=subprocess.STDOUT合并它们。 -
使用
raw = run('security ... -ga test', capture_output=True)自动捕获标准输出和标准错误。然后可以分别从raw.stdout和raw.stderr访问每个内容。
标签: python bash stdout io-redirection error-suppression