[Tutor] sensing EOF in Python 3.1
Cranky Frankie
cranky.frankie at gmail.com
Wed Nov 23 15:45:33 CET 2011
I'm reading in a pickled file. The program works but I'm having
trouble sensing end of file. Here's the program:
#
# pickle_in.py
# program to read in a pickled file
#
# Frank L. Palmeri
#
import pickle # import the pickle module
pickle_file = open("d:/Work/pickle_file", "rb") # open the pickled input file
read_file = pickle.load(pickle_file) # read the first input record
new_list=[] # create a new empty list
while pickle_file: # loop until end of file
for i in range(0, 4): # iterate for each field
new_list.append(read_file[i]) # append each field to
the new list
i = i + 1 # increment loop counter
print(new_list) # print the input
record from the new list
new_list=[] # initialize the new
list for the next record
read_file = pickle.load(pickle_file) # read the next record
in the input file
here's the error:
Traceback (most recent call last):
File "D:\MyDocs\Python\pickle_in.py", line 21, in <module>
read_file = pickle.load(pickle_file) # read the next record
in the input file
File "D:\Python31\lib\pickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
EOFError
--
Frank L. "Cranky Frankie" Palmeri
More information about the Tutor
mailing list