[Tutor] pickle problems
Richard D. Moores
rdmoores at gmail.com
Sun Aug 12 13:44:39 CEST 2012
On Sun, Aug 12, 2012 at 2:13 AM, Richard D. Moores <rdmoores at gmail.com> wrote:
.
> But what
> about case where factors.dat is empty? Is there a test for that?
I just discovered
os.path.getsize('factors.txt')
and that factors.txt has a size of 2 bytes when "empty".
(I changed the file extension to .txt so I could delete the contents.)
So I now have
===============================
D = {}
if os.path.getsize('factors.txt') == 2:
#The file exists, but is empty: leave it alone
pass
elif os.path.exists('factors.txt'):
#The file exists and is not empty: use it
f = open("factors.txt", 'rb')
data = pickle.load(f)
f.close
D = data
else:
# create the file and close it
f = open("factors.txt", 'wb')
f.close
==========================
which seems to work just fine for all cases.
Those lines would replace lines 74-78 in <http://pastebin.com/EZUKjPSk>
Dick
More information about the Tutor
mailing list