【问题标题】:EOF Exception when using input() on Python3在 Python3 上使用 input() 时出现 EOF 异常
【发布时间】:2018-07-01 21:51:33
【问题描述】:

我有以下函数抛出 EOF 异常:

def timeTracker(veiksmai, directory, cap, ipAddress):

while not is_non_zero(str(cap)):
    pass
print("Capture started confirmed")
print(veiksmai)
print(len(veiksmai))
print('|    Time, s |   Event   |', file=open(str(directory), 'a'))
startTime = time.time()
i = 0
while i != len(veiksmai):
    print('|    ' + str(round(time.time() - startTime)) + ' |   ' + veiksmai[i] + '   |\n', file=open(str(directory), 'a'))
    input("Press enter for next")
    i += 1

我在这一行遇到异常:

input("Press enter for next")

例外情况如下:

File "/home/dovydas/PycharmProjects/packet-capture/main_new.py", line 29, in timeTracker
input("Press enter for next")
EOFError: EOF when reading a line

我正在使用最新的 PyCharm 和 Python 3.6.5

我已经用谷歌搜索过这个问题,但据我所知,使用 PyCharm 应该没有问题。

我尝试创建一个空变量,例如:

var1 = input("Press enter for next")

但这并没有帮助。任何帮助表示赞赏!

更新:

这里是主要方法:

ipAddress = input("Enter ip address which you want to capture: ")
capDir = input("Enter directory of capture file: ")
timeDir = input("Enter directory of timeline file: ")


actions = []
userInput = ""
while userInput != "DONE":
    userInput = input('Enter next action: ')
    if userInput != 'DONE':
        actions.append(userInput)


if __name__ == '__main__':
    q = Queue()
    p = Process(target=capture, args=(ipAddress, capDir,))
    p.start()

    p2 = Process(target=timeTracker, args=(actions, timeDir, capDir, ipAddress,))
    p2.start()

【问题讨论】:

  • 你能告诉我们你是如何调用你的程序的吗?这里的问题是stdin 在调用input 之前关闭,这通常来自于没有像stdin 那样的交互式shell
  • 感谢您的评论。我在调用该方法之前添加了 sn-p。 p2 是过程。谢谢!

标签: python python-3.x exception eof


【解决方案1】:

问题在于multiprocessing.Process 显式关闭了stdin and opens /dev/null,而标准输入是input 的读取位置。您可以考虑改用subprocessthreads

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    相关资源
    最近更新 更多