This question already has answers here:
Catching an exception while using a Python 'with' statement
(4个答案)
2年前关闭。
我觉得很奇怪
可以举报
但我无法以某种方式捕捉到并继续。我是否在这里丢失了某些东西,还是真的希望您在使用open()之前使用isfile()或类似的东西?
(4个答案)
2年前关闭。
我觉得很奇怪
with open(file, 'r')
可以举报
FileNotFoundError: [Errno 2]
但我无法以某种方式捕捉到并继续。我是否在这里丢失了某些东西,还是真的希望您在使用open()之前使用isfile()或类似的东西?
最佳答案
使用try/except处理异常
try:
with open( "a.txt" ) as f :
print(f.readlines())
except Exception:
print('not found')
#continue if file not found
关于Python-使用open()除外(FileNotFoundError)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40640956/