【问题标题】:Python - How to catch read() errors reading filesPython - 如何捕获读取文件的 read() 错误
【发布时间】:2020-06-20 15:59:42
【问题描述】:

在这段代码中,我在尝试打开文件时检查错误,但如何在读取文件时检查错误?

try:
    with open("file.txt") as f:
        contents = f.read()
except FileNotFoundError:
    print("File Not Found")

【问题讨论】:

标签: python file


【解决方案1】:

您可以有多个 except 语句。因此,除了您这次要处理的错误之外,只需在当前之后添加另一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-06
    • 2023-04-10
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多