检索子进程的输出。调用()如何使进程的输出运行subprocess.call()?擦肩而过StringIO.StringIO对象为stdout给出此错误:Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call return Popen(*popenargs, **kwargs).wait()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 588, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 945, in _get_handles
c2pwrite = stdout.fileno()AttributeError: StringIO instance has no attribute 'fileno'>>>
3 回答

ibeautiful
TA贡献1993条经验 获得超5个赞
如果Python版本>=2.7,则可以使用子进程.检查输出它基本上可以实现您想要的结果(它以字符串的形式返回标准输出)。
简单示例(Linux版本,参见注):
import subprocessprint subprocess.check_output(["ping", "-c", "1", "8.8.8.8"])
请注意ping命令使用的是Linux符号(-c
(供计数)。如果您在Windows上尝试此操作,请记住将其更改为-n
同样的结果。
添加回答
举报
0/150
提交
取消