Home > Enterprise >  Can someone give an ELI5 explanation of what each component of 'with open()' is doing when
Can someone give an ELI5 explanation of what each component of 'with open()' is doing when

Time:04-03

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:

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

CodePudding user response:

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:

  • Related