class CarRecord: # declaring a class without other methods
def init (self): # constructor
self .VehicleID = ""
self.Registration = ""
self.DateOfRegistration = None
self.EngineSize = 0
self.PurchasePrice = 0.00
import pickle # this library is required to create binary f iles
ThisCar = CarRecord()
Car = [ThisCar for i in range (100)]
CarFile = open ('Cars.DAT', 'wb') # open file for binary write
for i in range (100) : # loop for each array element
pickle.dump (Car[i], CarFile) # write a whole record to the binary file
CarFile.close() # close file
CarFile = open( 'Cars.DAT','rb') # open file for binary read
Car = [] # start with empty list
while True: # check for end of file
Car.append(pickle.load(CarFile))# append record from file to end of l i st
CarFile.close()
最佳答案
这个怎么样?
while True: # check for end of file
try:
Car.append(pickle.load(CarFile)) # append record from file to end of l i st
except EOFError:
print('EOF!!!')
break