问题描述
我有一个名为 A
的二进制文件,它在调用时生成输出.如果我从 Bash shell 调用它,大部分输出被 A > 抑制./dev/null
.所有的输出都被 A &>/dev/null
我有一个名为 B
的 python 脚本需要调用 A
.我希望能够从 B
生成输出,同时抑制 A
的所有输出.
从 B
中,我尝试了 os.system('A')
, os.system('A >/dev/null')
、os.system('A &>/dev/null')
、os.execvp('...')
等. 但这些都没有抑制 A 的所有输出.
我可以运行 B &>/dev/null
,但这也会抑制 B
的所有输出,我不想要那样.
有人有什么建议吗?
如果你有 Python 2.4,你可以使用 子流程模块:
>>>导入子流程>>>s = subprocess.Popen(['cowsay', '你好'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]>>>印刷_______<你好 >-------^__^ (oo)\_______(__) )/||----w |||||I have a binary named A
that generates output when called. If I call it from a Bash shell, most of the output is suppressed by A > /dev/null
. All of the output is suppressed by A &> /dev/null
I have a python script named B
that needs to call A
. I want to be able to generate output from B
, while suppressing all the output from A
.
From within B
, I've tried os.system('A')
, os.system('A > /dev/null')
, and os.system('A &> /dev/null')
, os.execvp('...')
, etc. but none of those suppress all the output from A.
I could run B &> /dev/null
, but that suppresses all of B
's output too and I don't want that.
Anyone have suggestions?
If you have Python 2.4, you can use the subprocess module:
>>> import subprocess
>>> s = subprocess.Popen(['cowsay', 'hello'],
stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
>>> print s
_______
< hello >
-------
^__^
(oo)\_______
(__) )/
||----w |
|| ||
这篇关于禁止在 Python 调用中输出可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!