diff --git a/file_handling_3.py b/file_handling_3.py index 32a0a9c..1808991 100644 --- a/file_handling_3.py +++ b/file_handling_3.py @@ -22,18 +22,29 @@ # Creating / opening / closing binary files # -dfile = open("student_new.dat","ab") # b specifies to open it in binary mode -data = {} ## creating an empty dictionary +dfile = open("student_new.dat","rb+") # b specifies to open it in binary mode +data_w = {} ## creating an empty dictionary for writing purpose +data_r = {} ## creating an empty dictionary for reading purpose + ## reading the file +try : + print("file student_new.dat conains the following records :") + while True: ## sets to false if end of file is encountered + data_r = pickle.load(dfile) + print(data_r) +except EOFError: + dfile.close() + +dfile = open("student_new.dat","ab+") No_of_stdnt = int(input("enter the no of students in class :")) for i in range(No_of_stdnt): name = input("Enter the name of the student :") roll = int(input("enter the roll no of the student :")) marks = int(input("eneter the marks obtained by the student :")) - data['Name'] = name - data['Roll'] = roll - data['Marks'] = marks - ## the above three lines fillup the dictionary - pickle.dump(data,dfile) + data_w['Name'] = name + data_w[ 'Roll'] = roll + data_w[ 'Marks'] = marks + ## thedata_r above three lines fillup the dictionary + pickle.dump(data_w,dfile) dfile.close() diff --git a/student_new.dat b/student_new.dat index e71a9cb..eb078dc 100644 Binary files a/student_new.dat and b/student_new.dat differ