0

If I want to open a file, unpickle an object inside it, then overwrite it later, is it okay to just use

data = {} #Its a dictionary in my code
file = open("filename","wb")
data = pickle.load(file)
data["foo"] = "bar"
pickle.dump(data,file)
file.close()

Or would I have to use "rb" first and then use "wb" later (using with statements for each) which is what I am doing now. Note that in my program, there is a hashing algorithm in between opening the file and closing it, which is where the dictionary data comes from, and I basically want to be able to only open the file once without having to do two with statements

James Green
  • 125
  • 12

2 Answers2

1

If you want to read, then write the file, do not use modes involving w at all; all of them truncate the file on opening it.

If the file is known to exist, use mode "rb+", which opens an existing file for both read and write.

Your code only needs to change a tiny bit:

# Open using with statement to ensure prompt/proper closing
with open("filename","rb+") as file:
    data = pickle.load(file)  # Load from file (moves file pointer to end of file)
    data["foo"] = "bar"
    file.seek(0)     # Move file pointer back to beginning of file
    pickle.dump(data, file)  # Write new data over beginning of file
    file.truncate()  # If new dump is smaller, make sure to chop off excess data
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • trying this out now, fyi I forgot to do "pickle.dump(data,file)" in my original question, its updated now – James Green Oct 26 '18 at 20:59
  • @JamesGreen: K. I fixed it in the answer as well, as well as reordering the `truncate` step (truncating late reduces the odds of data loss if there is an error during pickling, and means the file system doesn't have to shrink the file at all if the new data is equal to or longer than the old). – ShadowRanger Oct 27 '18 at 01:06
  • I ended up using this method but without the "with statement" and instead just opening it at the beginning and closing it at the end, will change up the truncate order as you say, thank you – James Green Oct 27 '18 at 22:16
-1

You can use wb+ which opens the file for both reading and writing

This question is helpful for understanding the differences between each of pythons read and write conditions, but adding + at the end usually always opens the file for both read and write

Confused by python file mode "w+"

SPYBUG96
  • 1,089
  • 5
  • 20
  • 38
  • Does it overwrite at all? – James Green Oct 26 '18 at 20:14
  • @JamesGreen: Mode `wb+` definitely overwrites, so you don't want to do that if the data in the file matters to you (which it seems it does). If the file is known to exist, you'd was `rb+`, if the file might not exist (not the case here), mode `ab+` would work (though if you wanted to be able to read the contents *if* they exist, it would have to be followed by an immediate `seek` to position `0`). – ShadowRanger Oct 26 '18 at 20:18
  • Yeah I've ended up using "rb+" as I make sure to check if the file exists earlier in the program, thanks anywho – James Green Oct 26 '18 at 21:11