Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can someone give an ELI5 explanation of what each component of 'with open()' is doing when I pickle?

I’ve used this syntax many, many times to pickle a file, but the truth is I don’t understand what is actually happening. I just know that it gives me the result that I want.

with open('some_model_name.pickle', 'wb') as to_write:
   pickle.dump(some_model_object, to_write)

But what is actually happening? What if I don’t specify wb? What is as to_write?

Same with when I read my model back in:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

with open(path+'some_model_name.pickle', 'rb') as to_read:
  some_model_object = pickle.load(to_read)

>Solution :

The "wb" is shorthand for "write in binary mode". If you don’t specify "wb" you will get the default mode of open, which is read in text mode. In that case, the call to pickle.dump would then fail, because the file was opened in read-only mode and you try to write some bytes.

The binding as to_write means to assign the return-value of the open call to a local variable named to_write. A similar way to do the same thing, without using a context manager ("with statement") may look like:

try:
    to_write = open('some_model_name.pickle', 'wb')
    pickle.dump(some_model_object, to_write)
finally:
    to_write.close()

This is a simplified version, a more complete equivalent is given in the docs link at the end of this answer.

The second code-block shown in the question is analogous, except it’s opening the file "some_model_name.pickle" in "read binary" mode "rb", which is appropriate because pickle.load (reading bytes) is used here instead of pickle.dump (writing bytes).

Relevant docs links for the parts I’ve described:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading