[Tutor] Re: EOF error with pickle?

Lee Harr missive at hotmail.com
Wed Dec 24 22:34:02 EST 2003


>>You need to close the file and reopen it.
>>
>>Otherwise, I think the file pointer is still pointing to the end of the
>>file.
>
>
>The fix is correct, but the reason it works needs clarification.
>
>Doing close() on a file that we're writting is a necessity because file
>writes are 'buffered', and our operating system may not immediately write
>the bytes to disk till it feels it's worth it to flush the buffer.


Sure enough.

Looks like you may not even need to close the file ...

>>>import pickle
>>>l = range(100)
>>>f = file('temp', 'w')
>>>pickle.dump(l, f)
>>>f.flush()
>>>g = file('temp')
>>>l2 = pickle.load(g)
>>>l2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 
97, 98, 99]
>>>l3 = range(0, 10, 2)
>>>pickle.dump(l3, f)
>>>f.flush()
>>>l4 = pickle.load(g)
>>>l4
[0, 2, 4, 6, 8]


Although I am not sure how good an idea that would be.

_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*. 
http://join.msn.com/?page=features/featuredemail




More information about the Tutor mailing list