调用QR()函数后触发EOFError错误,寻求排查帮助
解决Python脚本调用QR()函数后出现EOFError的问题
运行Python脚本时触发EOFError: EOF when reading a line,仅在调用QR()函数时出现,注释掉result_QR = QR()后脚本可正常运行。以下是报错详情、完整代码及排查解决方法:
错误详情
Traceback (most recent call last): File "C:\Users\test\PycharmProjects\scanner\test.py", line 42, in <module> cont = input("new start y/n: ") ^^^^^^^^^^^^^^^^^^^^^^^^ EOFError: EOF when reading a line
完整代码
import colorama colorama.init() def code_138(): code_input = input("Scan Code 123: ") code = '990012341234567890' result = code_input == code if result == 'OK': print('CODE 138: ' + colorama.Fore.GREEN + 'TRUE' + colorama.Fore.RESET) print( 'CODE 138: ' + colorama.Fore.RED + 'FALSE' + colorama.Fore.RESET) def QR(): print("Scan Code QR. Ctrl-D or Ctrl-Z to next") code_input = [] while True: try: line = input() except EOFError: break code_input.append(line) code = ['SPC', '0200', '1', 'CH9300762011123456789', 'S', 'Test AG', 'Test X-Y-Z', '1', '8888', 'xxxxx', 'CH', '', '', '', '', '', '', '', '999.00', 'EUR', '', '', '', '', '', '', '', 'NON', '', 'Test', 'EPD'] result = code_input == code if result == 'OK': print('QR-Code: ' + colorama.Fore.GREEN + 'TRUE' + colorama.Fore.RESET) print( 'QR-Code: ' + colorama.Fore.RED + 'FALSE' + colorama.Fore.RESET) def main(): result_138 = code_138() result_QR = QR() if __name__ == '__main__': main() while True: cont = input("new start y/n: ") cont = cont.lower() while cont.lower() not in ("y", "n"): cont = input("new start y/n ") if cont == "n": print("Send") break if cont == "y": print('') print('') main()
完整报错输出
C:\Users\test\PycharmProjects\scanner\venv\Scripts\python.exe C:\Users\vonaht\PycharmProjects\handscanner\test.py Scan Code 123: 123 CODE 138: FALSE Scan Code QR. Ctrl-D or Ctrl-Z to next asdf asdf^D QR-Code: FALSE new start y/n: Traceback (most recent call last): File "C:\Users\test\PycharmProjects\scanner\test.py", line 43, in <module> cont = input("new start y/n: ") ^^^^^^^^^^^^^^^^^^^^^^^^ EOFError: EOF when reading a line Process finished with exit code 1
问题原因
当在QR()函数中输入Ctrl-D(Linux/macOS)或Ctrl-Z(Windows)时,系统会将标准输入流标记为**EOF(文件结束)**状态。后续所有调用input()的地方都会直接抛出EOFError,因为系统认为已经没有更多输入数据了。而注释掉QR()时,没有触发EOF标记,所以后续的input()能正常工作。
另外代码还有两个逻辑错误:
code_138()和QR()中,result是布尔值(True/False),但判断条件写成了result == 'OK',这个判断永远不会成立,导致绿色的TRUE永远不会输出。QR()函数中即使输入正确,也会执行后面的print(FALSE)语句,因为没有else分支。
解决方案
方案1:修改QR()的终止方式(推荐)
不用EOF来终止输入,改为用空行停止输入,避免标记输入流为EOF:
def QR(): print("Scan Code QR. 输入空行结束输入") code_input = [] while True: try: line = input() # 输入空行时终止循环 if not line: break except EOFError: # 兼容意外触发EOF的情况 break code_input.append(line) code = ['SPC', '0200', '1', 'CH9300762011123456789', 'S', 'Test AG', 'Test X-Y-Z', '1', '8888', 'xxxxx', 'CH', '', '', '', '', '', '', '', '999.00', 'EUR', '', '', '', '', '', '', '', 'NON', '', 'Test', 'EPD'] result = code_input == code if result: print('QR-Code: ' + colorama.Fore.GREEN + 'TRUE' + colorama.Fore.RESET) else: print('QR-Code: ' + colorama.Fore.RED + 'FALSE' + colorama.Fore.RESET)
同时修正code_138()的判断逻辑:
def code_138(): code_input = input("Scan Code 123: ") code = '990012341234567890' result = code_input == code if result: print('CODE 138: ' + colorama.Fore.GREEN + 'TRUE' + colorama.Fore.RESET) else: print('CODE 138: ' + colorama.Fore.RED + 'FALSE' + colorama.Fore.RESET)
方案2:在后续input()中捕获EOFError
如果必须保留EOF终止的逻辑,需要在主循环的input()调用中捕获EOFError,避免程序崩溃:
if __name__ == '__main__': main() while True: try: cont = input("new start y/n: ") except EOFError: print("\n输入结束,退出程序") break cont = cont.lower() while cont not in ("y", "n"): try: cont = input("new start y/n ") except EOFError: print("\n输入结束,退出程序") exit() cont = cont.lower() if cont == "n": print("Send") break if cont == "y": print('') print('') main()
内容的提问来源于stack exchange,提问作者Tobi
相关产品推荐
相关产品推荐

