如何在使用 Python 'with' 语句时捕获异常?
代码可以重写为,如下方式捕获异常
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
广告
代码可以重写为,如下方式捕获异常
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