2017-09-06 76 views
0

我只是想调用tracert并以字符串形式得到结果。看起来tracert调用工作正常,因为在发生错误之前结果显示在控制台上。调用子进程时,“ValueError:关闭文件上的I/O操作”

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 

UPDATE: 我试过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 
+1

如果我不得不猜测我会说这是因为'tracert'关闭了它的'stdin'。 – NPE

回答

0

不知何故stdout已关闭,我无法写入流。 所以这个问题与subprocess根本没有任何关系。

相关问题