可以重写代码以捕获异常,如下所示:
try: with open("myFile.txt") as f: print(f.readlines()) except: print('No such file or directory')
我们得到以下输出
C:/Users/nhooo1/~.py No such file or directory