[Tutor] pickle problems
Richard D. Moores
rdmoores at gmail.com
Sun Aug 12 11:13:32 CEST 2012
On Sun, Aug 12, 2012 at 1:00 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 12/08/12 03:43, Richard D. Moores wrote:
>
>> ===========
>> if "factors.dat":
>
> This is testing if the string is True, which it always is.
> I assume you intended something like
>
> if os.path.exists('factors.dat'):
>
>
>> f = open("factors.dat", 'rb')
>> data = pickle.load(f)
>> f.close
>> D = data
>> else:
>> f = open("factors.dat", 'wb')
>
>
> Not sure there is any point in opening a new file at this point. you are
> trying to populate data, but if there's no file there is no data so instead
> of opening the file you want something like data = {}
Great! OK, I now have
=========================
D = {}
if os.path.exists('factors.dat'):
f = open("factors.dat", 'rb')
data = pickle.load(f)
f.close
D = data
else:
f = open("factors.dat", 'wb')
f.close
===========================
Which takes care of the case where factors.dat is missing. But what
about case where factors.dat is empty? Is there a test for that?
When factors.dat exists, but is empty, I get
==============================
Traceback (most recent call last):
File "C:\P32Working\pickle_attempt_for_web3a.py", line 78, in <module>
data = pickle.load(f)
EOFError
Process terminated with an exit code of 1
==============================
Dick
More information about the Tutor
mailing list