立即学习:https://edu.csdn.net/course/play/10443/223257?utm_source=blogtoedu
1. file = open(‘123.txt’,‘r+’)# r+ :去读一个文件, 如果能打开就可以 写入
2.try:#尝试运行需要运行的语句,如有出错则按照安排好的预案执行。
假设目前打开的路径没有123.txt 这个文件
file = open(‘123.txt’,‘r+’)
except Exception as e:#将Exception简称为e ,并把错误信息保存其中
print(e)
response=input(“create file?”)
if (response=“yes”):
with open(“123.txt”,‘w’)as f:#以写入方式打开,文件不存在即创建
else:
pass (other command)
else:# try 后 没有发生错误则运行如下语句