Graceful detection of EOF

Jeremy Jones zanesdad at bellsouth.net
Thu Oct 7 20:47:57 CEST 2004


MickeyBob wrote:

>How does one detect the EOF gracefully? Assuming I have a pickle file
>containing an unknown number of objects, how can I read (i.e.,
>pickle.load()) until the EOF is encountered without generating an EOF
>exception?
>
>Thanks for any assistance.
>MickeyBob
>
>  
>
pickle.load() should handle that for you, shouldn't it?  You just pass 
it a file-like object.  Pickle.load() should execute a .read() on it.


In [9]: d = {}

In [10]: for i,j in enumerate(range(10)):
   ....:     d[i] = j
   ....:

In [11]: d
Out[11]: {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}

In [12]: pickle.dump(d, open('d.p', 'w'))

In [13]: dd = pickle.load(open('d.p', 'r'))

In [14]: dd
Out[14]: {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}

Or are you trying to do something different?



Jeremy Jones
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041007/4d564bf3/attachment.html>


More information about the Python-list mailing list