我有一个字符串列表,随着迭代的不断变化。我想将元素列表写入文本文件。当列表发生变化时,我希望更新文本文件而不覆盖旧数据。
我尝试使用append方法来完成这个操作,但是我不知道为什么每次运行代码时都会出现错误。它给出了以下错误:
AttributeError:'_io.TextIOWrapper‘对象没有属性’追加‘
matches = ['Steve', 'Kaira', 'Wokes']
with open('textfile.txt','a') as f:
for match in matches:
f.append(match)
发布于 2019-06-13 11:30:47
文件对象没有任何属性append
是导致错误的原因。在python中没有文件对象的名为append()
的函数。相反,在附加模式下使用write()
。请参阅此链接:io.TextIOWrapper' object has no attribute 'append'
参见此示例:
urls = open("urlfile.txt","a")
for image_url in image_urls:
urls.write(image_url + "\n")
urls.close()
https://stackoverflow.com/questions/56579387
复制相似问题