[Tutor] pickle problems

Peter Otten __peter__ at web.de
Sun Aug 12 22:58:45 CEST 2012


Richard D. Moores wrote:

> f = open("factors.txt", 'rb')
> data = pickle.load(f)
> f.close

f.close looks up the close method but doesn't invoke it; you need f.close().
Alternatively use a with statement:

with open("factors.txt", "rb") as f:
    data = pickle.load(f)

This will close the file even if an exception occurs when trying to unpickle 
the data.



More information about the Tutor mailing list