[Tutor] Fwd: pickle module error in source code
Hi! My name is Shreya Marwaha. I’m class 12th student in India. I’m using 2.7.5 python version [MSC v.1500 64 bit (AMD64)] on win32 as instructed by CBSE (central board of secondary education). I’ve been having problem in using pickle module. It keeps on showing error in the source code. I’ve attached two files: 1. Prax.py file with error1.txt 2. Video.py file with error.txt Prax.py file is a simple problem, the first program I tried with pickle module. Video.py is my project which I’ll be submitting at the end of my year at school i.e. February 2015. I’ve attached two text documents in which both are showing the error occurring in both the problems. The errors in both the programs are similar. Please help me in this context. -Shreya Marwaha # File name: ...\\Source_XII\Project\Video.py import os from pickle import load, dump import datetime import string MFile = Master.dat File1 = Cassettes.dat File2 = Balance.dat File3 = Customer.dat Cdate = datetime.datetime.now() # Current date and time # Class for date class Cast_Date: def __init__(self): self.dd = Cdate.day self.mm = Cdate.month self.yy = Cdate.year class Master: # Constructor def __init__(self): self.Cast_Code = 0 # cassette/CD code - (Like, 1, 2, 3, etc.) self.Cast_Name = # Title of the cassette/CD self.Cast_Comp = # cassette/CD company self.Cast_Price = 0 # Price per cassette/CD def Check_Code(self, C_Code): MList = list() TRec = list() Flag = False # To check if Cast_Code is in Master.dat or not if os.path.isfile(MFile): Mobj = open(MFile, 'rb') try: while True: MRec = [] # For extracting Master.dat records MRec = load(Mobj) if (C_Code == MRec[0]): TRec = MRec MList.append(MRec[0]) except EOFError: pass for i in range(len(MList)): if (C_Code == MList[i]): Flag = True break Mobj.close() # Flag for Master data entry and TRec for Cassette data entry return Flag, TRec # For Master data entry def Master_Entry(self): TRec = list() # A temporary list to store master record print(Add Master Cassette/CD); ch ='Y' while ch=='Y': while True: self.Cast_Code = int(input(Cassette/CD Code (1/2/3...) # )) Flag, TRec = self.Check_Code(self.Cast_Code) if (Flag == False): while True: self.Cast_Name = input(Cassette/CD Name : ) if (self.Cast_Name == 0 or len(self.Cast_Name) 25): print(Cassette/CD Name should not greater than 25) else: break while True: self.Cast_Comp = input(Company Name : ) if (self.Cast_Comp == 0 or len(self.Cast_Comp) 25): print(Company Name should not greater than 25) else: break while True: self.Cast_Price = float(input(Individual Cassette/CD price : )) if (self.Cast_Price = 0): print(Enter valid price for Cassette/CD) else: break; with open(MFile, 'ab+') as Mobj: if not Mobj: print (MFile, does not created) else: # Appends data into a sequnce object MList = list() MList.append(self.Cast_Code) MList.append(self.Cast_Name) MList.append(self.Cast_Comp) MList.append(self.Cast_Price) # Write data into binary file dump(MList, Mobj) else: print (Code, self.Cast_Code, is already in 'Master.dat' file) ch = input(Add new Cassette/CD code? Y/N: ) ch = ch.upper() if ch=='Y': continue else: break def Master_Display(self): if not os.path.isfile(MFile): print (MFile, file does not exist) else: Mobj = open(MFile, 'rb') print (\nCassette/CD Master Report) print (= * 25) print ({0:7} {1:30} {2:20} {3:8}.format( Code, Cassette/CD Name, Company Name, Price)) print (- * 70)
Re: [Tutor] Fwd: pickle module error in source code
On 01/01/15 14:06, Vedhi Shreya Marwaha wrote: Hi! My name is Shreya Marwaha. I’m class 12th student in India. I’m using 2.7.5 python version [MSC v.1500 64 bit (AMD64)] on win32 as instructed by CBSE (central board of secondary education). I’ve been having problem in using pickle module. OK, welcome. It keeps on showing error in the source code. I’ve attached two files If the files are short its better to just paste the contents into your mail. Not everyone can (or is allowed to) read attachments. : 1. Prax.py file with error1.txt 2. Video.py file with error.txt Prax.py file is a simple problem, the first program I tried with pickle module. OK, Let's stick with that initially. The error says: Traceback (most recent call last): File C:\Users\Home\Downloads\prax.py, line 25, in module a=pickle.load(f) File C:\Python27\lib\pickle.py, line 1378, in load return Unpickler(file).load() File C:\Python27\lib\pickle.py, line 858, in load dispatch[key](self) File C:\Python27\lib\pickle.py, line 880, in load_eof raise EOFError EOFError The relevant code is: import pickle a=student() f=open(student.log,wb+) In general, don;t use the + modifier with files. It usually creates more confusion. Just open the file for reading after you finish writing. And try very hard not to mix reading and writing to the same file. a.input() pickle.dump(a,f) Here you have written your data to the file. The file 'cursor' is now pointing at the end of the file. a=pickle.load(f) Here you try to read from the file but the cursor is already at the end, so you get an end of file error. To make it work you could rewind the cursor to the beginning of the file using seek(). But it would be much safer to simply close the file after the dump() and then reopen the file for reading. That avoids a whole bunch of potential complexity and bugs just waiting to bite. Also, consider using the 'with' structure for handling files, it is generally safer than open/close: with open('student.log','wb') as f: a.input() pickle.dump(a,f) with open('student.log','rb') as f: a = pickle.load() 'with' ensures the files are closed after use. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor