元のファイルに追加する必要がありますが、最初に内容を消去してください(元のファイルにはピクルスのコンテンツがあると仮定します)。 あなたはそれが内のファイルを読み込み、それを書き換える必要はありませんので、単純に、新しい漬け物
import pickle
#create the initial file for test purposes only
obj = {"a":"b","c":"d"}
with open("file.txt","wb") as f:
pickle.dump(obj, f)
#reopen and unpickle the pickled content and read to obj
with open("file.txt","rb") as f:
obj = pickle.load(f)
print(obj)
#add to the dictionary object
obj["newa"]="newb"
obj["newc"]="newd"
with open("file.txt","wb") as f:
pickle.dump(obj, f)
#reopen and unpickle the pickled content and read to obj
with open("file.txt","rb") as f:
obj = pickle.load(f)
print(obj)
関連:https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-fileただし、ピクルファイルで動作するかどうかを確認してください。 – Evert