EOFError:读取行时的 EOF (Python 3.11)

EOFError: EOF when reading a line (Python 3.11)

提问人:Gregory Buffard 提问时间:8/3/2022 最后编辑:Gregory Buffard 更新时间:8/3/2022 访问量:893

问:

你好 StackOverflow 社区, 由于某种原因,我正在努力解决我在代码中遇到的错误,我已经尝试了一些可能的方法来解决它,例如try/except 函数,但无论如何都无法解决它,所以我问你是否可以帮助我解决这个问题。EOFError: EOF when reading a line

这是错误:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.181.0_x64__qbz5n2kfra8p0\Lib\multiprocessing\process.py", line 314, in _bootstrap
    self.run()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.181.0_x64__qbz5n2kfra8p0\Lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\grego\Documents\GitHub\notes-app\Noti.py", line 17, in checkPath
    takeQuickiPy()
  File "C:\Users\grego\Documents\GitHub\notes-app\Noti.py", line 23, in takeQuickiPy
    quickiPy = input(">    ")
               ^^^^^^^^^^^^^^
EOFError: EOF when reading a line

以下是完整代码:

import os
import multiprocessing as mp
from os import listdir
import simple_chalk as chk

path = "./QuickiPies"
pathBoolean = os.path.exists(path)
quickiPy = "null"


def checkPath():
    if pathBoolean == False:
        os.mkdir(path)

    takeQuickiPy()


def takeQuickiPy():
    print(chk.greenBright.bold(
        "Welcome to NotiPy! By pressing Ctrl+Shift+S your QuickiPy will be saved to the QuickiPy directory. Open an existing QuickiPy with Ctrl+Shift+O."))
    quickiPy = input(">    ")


def checkShortcut():
    while True:
        if kd.is_pressed("ctrl+shift+o"):
            listDir()
            break
        elif kd.is_pressed("ctrl+shift+s"):
            saveQuickiPy()
            break


def listDir():
    print("Choose your QuickiPy:")
    for i in listdir(path):
        print(i)


def saveQuickiPy():
    file = open(path + "/" + "note.txt", "w")
    file.write(quickiPy)
    file.close()
    print(chk.greenBright.bold("QuickiPy saved!"))


if __name__ == '__main__':
    jobs = []
    process1 = mp.Process(target=checkPath)
    jobs.append(process1)
    process2 = mp.Process(target=checkShortcut)
    jobs.append(process2)
    process1.start()
    process2.start()

PS:该程序应该是一个类似记事本终端的程序,提示您写入输入,但是当按Ctrl + Shift + S时,程序会将输入保存到文件中,如果按Ctrl + Shift + O,程序将列出目录中所有可用打开的文件(此功能暂时未实现)。

感谢您提供任何可能的解决方案。

python-3.x 输入 EOF

评论

0赞 Mirrah 8/3/2022
您需要将输入拉入主线程。你可能想检查这个 stackoverflow.com/questions/62949599/... stackoverflow.com/questions/47141057/......

答:

0赞 anmabe 8/3/2022 #1

不应使用多处理,而应使用线程处理,因为多处理不会等到用户输入函数时才使用。input()

您应该将多处理视为同时处理多个进程的单个处理线,而无需等待任何这些进程的长任务完成。另一方面,线程就像拥有各种处理线,每条线都以自己的速度运行,允许等待单个处理任务完成。

由于您使用的是多处理,因此循环和函数发生冲突,并且 python 在此过程中以 EOF(文件结束)错误进行响应。您应该按如下方式调整代码:while Trueinput()input()

import os
from threading import Thread
from os import listdir
import simple_chalk as chk

path = "./QuickiPies"
pathBoolean = os.path.exists(path)
quickiPy = "null"


def checkPath():
    if pathBoolean == False:
        os.mkdir(path)

    takeQuickiPy()


def takeQuickiPy():
    print(chk.greenBright.bold(
        "Welcome to NotiPy! By pressing Ctrl+Shift+S your QuickiPy will be saved to the QuickiPy directory. Open an existing QuickiPy with Ctrl+Shift+O."))
    quickiPy = input(">    ")


def checkShortcut():
    while True:
        if kd.is_pressed("ctrl+shift+o"):
            listDir()
            break
        elif kd.is_pressed("ctrl+shift+s"):
            saveQuickiPy()
            break


def listDir():
    print("Choose your QuickiPy:")
    for i in listdir(path):
        print(i)


def saveQuickiPy():
    file = open(path + "/" + "note.txt", "w")
    file.write(quickiPy)
    file.close()
    print(chk.greenBright.bold("QuickiPy saved!"))


if __name__ == '__main__':
    jobs = []
    process1 = Thread(target=checkPath)
    jobs.append(process1)
    process2 = Thread(target=checkShortcut)
    jobs.append(process2)
    process1.start()
    process2.start()