【发布时间】:2023-04-01 18:08:01
【问题描述】:
此代码总是进入 except 部分
to_add = "text to add"
try:
with open ('text.txt','r') as txt:
txt.write('text to add'+to_add')
print("done")
except:
to_add== 0 or None or ''
print("unable to write to file")
【问题讨论】:
-
这里有一个额外的报价
txt.write('text to add'+to_add')。删除最后一个。 -
请正确格式化您的代码,click here to learn how。
-
要写入文件,请使用
open(file_name, 'w')或附加open(file_name, 'a')。您已使用'r'选项以读取模式打开它。
标签: python python-3.x with-statement