try: <语句> except <name>: <语句> #如果在try部份引发了名为'name'的异常,则执行这段代码 else: <语句> #如果没有异常发生,则执行这段代码
##错误判断的代码
##其中,想让文件写东西,在open一个文件的时候需要给r+
try:
file = open('/home/zbx/桌面/abcd','r+')
except Exception as e:
print("there is no file names as abcd")
input_text = input("Do you want to new a file ?")
if input_text == 'y':
file = open('/home/zbx/桌面/abcd','w')
else:
pass
else:
file.write('ssss')
file.close()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有