【问题标题】:Using exit(), I get "ValueError: I/O operation on closed file."使用 exit(),我得到“ValueError: I/O operation on closed file”。
【发布时间】:2020-06-03 12:42:49
【问题描述】:

我正在学习 Python,并且在尝试构建身份验证器(登录)时遇到了这个错误。

credentials = {
    "Peter" : "123",
    "Chris" : "Ga1",
    "Michael" : "uwu",
    "Steve" : "xdx10"
}
entered_username = None
entered_password = None
username_errors = 0
password_errors = 0

while True:
    entered_username = input("Please enter your username: ")
    entered_password = input("Please enter your password: ")
    try:
        if entered_password != credentials[entered_username]:
            password_errors += 1
            if password_errors == 5:
                print("Your account has been locked for security reasons.")
                exit()
            print("Wrong username or password, try again.")
            continue
        else:
            print("success 1")
    except:
        username_errors += 1
        if password_errors + username_errors == 5:
            print("Please try again at a later time.")
        print("Wrong username or password, try again.")
        continue
    print("Welcome {}! Your password is {}.".format(entered_username, entered_password))
    break

所以有问题的 exit() 大约是代码的一半,在 if 块的 if 块中 try 块。

运行代码并输入正确密码五次失败应该会显示“您的帐户已因安全原因被锁定”。然后退出代码。但是,它会进行这种追溯。

如果我拿走exit(),回溯就会消失。

我不知道是不是因为try/except,但我已经尝试解决这个问题了一个小时,我快要死在里面了(ಠ_ಠ)。

编辑:没有粘贴回溯(facepalm)

Your account has been locked for security reasons.
Wrong username or password, try again.
Please enter your username: Traceback (most recent call last):
  File "C:/Users/myname/PycharmProjects/stem1401python/py200603/login_form_v3/stem1401_python_login_form_v3.py", line 42, in <module>
    entered_username = input("Please enter your username: ")
ValueError: I/O operation on closed file.

我还要补充一点:我不知道为什么它在 exit() 之后仍然打印出东西。就像在print("Your account has been locked for security reasons.") 之后有exit()

【问题讨论】:

  • 你的文件IO代码在哪里?您的异常似乎与文件有关
  • 尝试用 sys.exit() 替换出口(也可以导入 sys....)
  • 请将完整的错误回溯添加到您的问题中。
  • sys.exit() 通过抛出异常来工作。您的 except: 块丢弃所有异常。永远不要使用纯 except:,始终只捕获您期望的特定异常。
  • 也可以试试os._exit()

标签: python python-3.x runtime-error traceback try-except


【解决方案1】:
Your account has been locked for security reasons.
Wrong username or password, try again.
Please enter your username: Traceback (most recent call last):
  File "C:/Users/myname/PycharmProjects/stem1401python/py200603/login_form_v3/stem1401_python_login_form_v3.py", line 42, in <module>
    entered_username = input("Please enter your username: ")
ValueError: I/O operation on closed file.

上面的错误是由于pycharm ide当它到达文件中的exit()行时它只是closes the file not terminates the running process所以当它到达 continue 时它会回到循环并尝试获取 input()它@ 987654325@,所以如果你使用idle运行文件,它将成功运行而没有回溯,因为it will automatically kill the running process when file is exited,或者如果你想在pycharm中运行代码只需使用os.abort()而不是exit(),因为它将中止进程

credentials = {
    "Peter" : "123",
    "Chris" : "Ga1",
    "Michael" : "uwu",
    "Steve" : "xdx10"
}
entered_username = None
entered_password = None
username_errors = 0
password_errors = 0
import os
while True:
    entered_username = input("Please enter your username: ")
    entered_password = input("Please enter your password: ")
    try:
        if entered_password != credentials[entered_username]:
            password_errors += 1
            if password_errors == 5:
                print("Your account has been locked for security reasons.")
                os.abort()
            print("Wrong username or password, try again.")
            continue
        else:
            print("success 1")
    except:
        username_errors += 1
        if password_errors + username_errors == 5:
            print("Please try again at a later time.")
        print("Wrong username or password, try again.")
        continue
    print("Welcome {}! Your password is {}.".format(entered_username, entered_password))
    break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2015-11-27
    • 2021-05-19
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多