mardi 18 décembre 2018

How to fix an error in a random access file program trying to process records in a file using the hash function?

Hi I am learning to use the hash function to read and write records to a random access file. I have taken some code from a book that illustrates this but I am struggling to get the code to work. The error messages I get from interpreting the code is that on line 22 I have invalid code that is the CarFile.close() command.

import pickle # this library is required to create binary files

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

ThisCar = CarRecord()

ThisCar.VehicleID = 'Mercedes'

CarFile = open('Cars.DAT','rb+')    #open this file for binary read and write
Address = hash(ThisCar.VehicleID)

CarFile.seek(Address)
pickle.dump(ThisCar, CarFile)       #write a whole record to the binary file

CarFile.close()

CarFile = open('Cars.DAT','rb')     #open file for binary read
Address = hash(VehicleID)
CarFile.seek(Address)
ThisCar = pickle.load(CarFile)      #load record from file

CarFile.close()




Aucun commentaire:

Enregistrer un commentaire