[Tutor] pickle.load() all dict

Knacktus knacktus at googlemail.com
Sun May 16 15:05:18 CEST 2010


Am 16.05.2010 14:45, schrieb Yutao Deng:
> LOL, try...except is a good idea.
> i fix it  lick this:
>
> ############
> with open("data2.pickle","rb") as file_stream:
>     c = 0
>     while True:
>         try:
>             i = cPickle.load(file_stream)
>             print i
>             c += 1
>         except:
>             print "Numer of pickled objects is %s." %c
>             break
> #############
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>    
Just a remark on your except statement: You should not use "except:" 
without specifying a concrete exception class, because all kind of 
exceptions are then handled by the except block (and therefore hidden). 
If for example an IOError occurs while reading the file (for what ever 
reason...), it would be handled by your except block and you would never 
know that there was a problem reading the file. If you use "except 
EOFError:" the exception block is evaluated only when the EOFError 
occurs. All other exceptions are raised as usual.

Best regards,

Jan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100516/04ebb4c6/attachment.html>


More information about the Tutor mailing list