Python Tutorial | Python Data File Handling 13

Binary File to maintain student records

 

Student Data maintained :
roll number
name
per

To Add record in a Binary File

Points

1. if the file does not exist the create a binary file
2. if the file exits then open the file in append mode
3. Take input for roll, name and per.
4. store them in a variable rec
5. write the record in the binary file using pickle.dump()
6. close the file

import pickle
import os

#function definition
def add_record():
     try:
          if os.path.isfile("stud"):
               f=open("stud","ab")
          else:
               f=open("stud","wb")
          roll=int(input("Enter roll no "))
          name=input("Enter name ")
          name=name.upper()
          per=float(input("Enter per "))
          rec=[roll,name,per]
          pickle.dump(rec,f)
          print("Record added in file")
     except EOFError:
          f.close()

#function calling
add_record()

program with validations

import pickle
import os

#function definition
def add_record():
     try:
          if os.path.isfile("stud"):
               f=open("stud","ab")
          else:
               f=open("stud","wb")
          """
          roll=int(input("Enter roll no "))
          name=input("Enter name ")
          name=name.upper()
          per=float(input("Enter per "))
          """
          #roll no input
          while True:
               z=0
               roll=input("Enter roll no (4 digit) :")
               if not roll.isdigit():
                    print("Rollno Has to be Numeric")
                    input("Press Enter to input again...")
                    z=1
                    continue
               if not (int(roll)>=1000 and int(roll)<=9999):
                    print("Rollno Has to be in 4 digitsd")
                    input("Press Enter to input again...")
                    z=1
                    continue
               if z!=1:
                    break

          # Name input
          while True:
               z=0
               name=input("Enter Name :")
               if not name.isalpha():
                    print("Name Has to be Alphabetic")
                    input("Press Enter to input again ... ")
                    z=1
                    continue
               if z!=1:
                    break
     
          # per input
          while True:
               z=0
               per=input("Enter per  :")
               if not per.isdigit():
                    print("Per Has to be Numeric")
                    input("Press Enter to input again...")
                    z=1
                    continue
               if not (int(per)>=0 and int(per)<=100):
                    print("Per Has to be >=0 and <=100")
                    input("Press Enter to input again...")
                    z=1
                    continue
               if z!=1:
                    break
          rec=[int(roll),name.upper(),float(per)]
          pickle.dump(rec,f)
          print("Record added in file")
     except EOFError:
                    f.close()

#function calling
add_record()