如何在使用 Python 'with' 语句时捕获异常?
pythonserver side programmingprogramming更新于 2025/12/26 23:08:17
可以重写代码来捕获异常,如下所示:
try:
with open("myFile.txt") as f:
print(f.readlines())
except:
print('No such file or directory')
我们得到以下输出
C:/Users/TutorialsPoint1/~.py No such file or directory
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

