在 Python3 上使用 input() 时的 EOF 异常

EOF Exception when using input() on Python3

提问人:Dovydas Ražukas 提问时间:7/2/2018 最后编辑:Dovydas Ražukas 更新时间:7/2/2018 访问量:361

问:

我有以下函数向我抛出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()
python-3.x 异常 eof

评论

2赞 Matthew Story 7/2/2018
你能告诉我们你是如何称呼你的程序的吗?这里的问题是在调用之前关闭,这通常是由于没有交互式 shell 作为stdininputstdin
0赞 Dovydas Ražukas 7/2/2018
谢谢你的评论。我已经添加了有关在调用该方法之前发生的情况的代码片段。p2 是过程。谢谢!

答:

0赞 Matthew Story 7/2/2018 #1

问题是显式关闭 stdin 并打开 /dev/null,标准输入是读取的来源。您可以考虑改用 OR。multiprocessing.Processinputsubprocessthreads