【发布时间】:2017-09-06 10:54:17
【问题描述】:
我只是想调用tracert 并将结果作为字符串获取。 tracert 调用似乎工作正常,因为在错误发生之前结果显示在控制台上。
import subprocess
import sys
proc = subprocess.Popen(["tracert", "localhost"], stderr=subprocess.PIPE, stdin=subprocess.PIPE)
print proc.communicate()[0]
错误:
Traceback (most recent call last):
File "-", line 207, in <module>
main()
File "-", line 203, in main
tracert("localhost")
File "-", line 188, in tracert
print proc.communicate()[1]
ValueError: I/O operation on closed file
更新:
我尝试了logging 而不是print,因为它是无缓冲的。
2017-09-07 09:11:57,165 - [test] - INFO -
Routenverfolgung zu localhost [::1]
�ber maximal 30 Hops:
1 <1 ms <1 ms <1 ms localhost [::1]
Ablaufverfolgung beendet.
Traceback (most recent call last):
File "-\lib\logging\__init__.py", line 885, in emit
self.flush()
File "-\logging\__init__.py", line 845, in flush
self.stream.flush()
ValueError: I/O operation on closed file
【问题讨论】:
-
如果我不得不猜测我会说这是因为
tracert关闭了它的stdin。
标签: python subprocess ioerror