Program to Displays the Records of Student Having Marks Between 50 & 70 in Binary File.

Q. Write a program to display the records of student having marks between 50 & 70 in Binary File.

file: bin.dat
programming-exercise-on-binary-files

Solution:

#Displaying the Records of Student Having Marks between 50 & 70

import pickle

def read_data():
    f = open("bin.dat",'rb')
    s = pickle.load(f)
    for i in s:
        if i[2] <= 50 or i[2] <= 70:
            print(i)
        else:
            pass

read_data()


Output:

# [1600, 'Raj', 20]
# [1601, 'Rajat', 23]

count-total-number-of-characters-in-a-text-file
Explanation: Here we have defined a function read_data(). Inside read_data() function we have also created a file object “f” and opened our binary file in read mode ie. “rb” mode.

In next step, we have initialized all the values to variable “s“. And used load() function to read our binary file. The load() function return the output in structure format from binary encoding.

After that we used for loop to iterate over each entry present inside our list “x“, as marks data available at second index hence we used i[2] list indexing for checking the condition. Hence we get the final result.

  1. Programming questions on Text Files

    1. WAP to define a method to read text document line by line.
    2. WAP to define a method in python to read lines from a text file starting with an alphabet F.
    3. WAP to define a method to count number of lines starting with an alphabet F.
    4. WAP to define a method which display only those lines starting with an alphabet A or F.
    5. WAP to define a method to display only those lines which are bigger than 50 characters.
    6. WAP to define a method to count total number of characters in our text file.
    7. WAP to define a method to count total numbers of word available in our text file.
    8. WAP to define a method which counts the occurrence of particular word in a text file.
    9. WAP to define a method which only print the words having more than 5 characters.
    10. WAP to define a method which counts the occurrence of “is”, “to” in a text file.

    Programming questions on Binary Files

    1. WAP to define a method which displays only those student records who secured grade A.