Python 为什么我会犯这个错误'_io.TextIOWrapper';对象没有属性';追加';

Python 为什么我会犯这个错误'_io.TextIOWrapper';对象没有属性';追加';,python,append,Python,Append,为了避免这个错误,我已经做了很多事情,但我已经准备好了。有人知道我为什么老是犯这个错误吗 myList =[n, weather, wind, other, avgscore] with open("data.txt", 'w') as f: for s in myList: f.append(str(s) + '\n') print("Thank you, your data was logged") 您需要使用write()而不是ap

为了避免这个错误,我已经做了很多事情,但我已经准备好了。有人知道我为什么老是犯这个错误吗

myList =[n, weather, wind, other, avgscore]
    with open("data.txt", 'w') as f:
        for s in myList:
            f.append(str(s) + '\n')
    print("Thank you, your data was logged")

您需要使用
write()
而不是
append()

如果要将数据附加到已写入的文件中而不覆盖该文件,请执行以下操作:

您需要在追加模式下打开文件,方法是将“
a
”或“
ab
”设置为 模式

输出

n
weather
wind
other
avgscore
n2
weather2
wind2
other2
avgscore2

f.append()
Uh?或者您的意思是
f.write()
?错误本身是不言自明的。您试图附加到
TextIoWrapper
对象,而不是
列表
对象。我尝试了f.write,但它一直覆盖文件中已有的文本。@elliottbellamy看到下面编辑的答案了吗?太好了@肮脏的
myList =['n2', 'weather2', 'wind2', 'other2', 'avgscore2']
with open("list.txt", 'a') as f:
    for s in myList:
        f.write(str(s) + '\n')
print("Thank you, your data was logged")
n
weather
wind
other
avgscore
n2
weather2
wind2
other2
avgscore2