【问题标题】:"ValueError: I/O operation on closed file" when calling subprocess调用子进程时出现“ValueError:对已关闭文件的 I/O 操作”
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-09-27
    • 2016-07-21
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    相关资源
    最近更新 更多