How to close the file after pickle.load() in python

It's better to use a with statement instead, which closes the file when the statement ends, even if an exception occurs:

with open("filename.pkl", "wb") as f:
    pickle.dump(dictname, f)
...
with open("filename.pkl", "rb") as f:
    dictname = pickle.load(f)

Otherwise, the file will only get closed when the garbage collector runs, and when that happens is indeterminate and almost impossible to predict.


Using the with statement is the better approach, but just to be contrary, if you didn't use with, you should retain a file handle… and close from there.

f = open('filename.pkl', 'wb')
pickle.dump(dictname, f)
f.close()

and in the other script:

f = open('filename.pkl','rb')
dictname = pickle.load(f)
f.close()

This is essentially what with is doing for you.

However… if you were stuck (for whatever reason), with the code you originally posted, and to answer your original question… yes, the garbage collector will close it for you at some unspecified time in the future. Or you could possibly track down a reference to the file object using the gc module, and then close it. There are a few codes out there that might help you do this, for example: https://github.com/uqfoundation/dill/blob/master/dill/pointers.py

However, with and f.close() are much much more preferred, and you should avoid tracing through the gc module unless you really are in a pickle.


I guess people just want to avoid manually closing file. Use pickle.loads() / dumps() and pathlib methods can do so:

import pickle
import pathlib # Must be Python 3.5 or above

# one line to load pkl, auto closing file
data = pickle.loads(pathlib.Path('path/to/pkl').read_bytes())

# also one line to dump, auto closing file
pathlib.Path('path/to/save').write_bytes(pickle.dumps(data))