1 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
您正在打開(kāi),但沒(méi)有讀取文件。您創(chuàng)建一個(gè)新列表而不是附加到現(xiàn)有列表。
這是如何做到的:
with open('guilds.yaml', 'r+') as f:
# load the content
content = yaml.safe_load(f)
# append the new id to the existing list
content["connected_guilds"].append(guild_id)
# reset the position in the file (it's at the end since we read the file)
f.seek(0)
# write the updated YAML to the file
yaml.dump(content, f)
# throw away any (old) content of the file after the current position,
# which is at the end of the YAML we just wrote.
# since we added more content, it's unlikely that there is more content here,
# but not impossible!
f.truncate()
添加回答
舉報(bào)