如果文件不存在,则捕捉异常,进行新建该twst.txt文件并写入一些话,最后关闭文件(并模拟错误)
收起
当前问题酬金
¥ 0 (可追加 ¥500)
支付方式
扫码支付
支付金额 15 元
提供问题酬金的用户不参与问题酬金结算和分配
支付即为同意 《付费问题酬金结算规则》
file1 = 'test.txt' try: f = open(file1, 'r') print(f.read()) f.close() except: print(f'文件{file1}不存在') file2 = 'twst.txt' try: f = open(file2, 'w') f.write('asdfjkl123456') f.close() print('写入成功') except: print(f'文件{file2}不存在,或写入错误')
报告相同问题?