Board Q&A · Class 12 Computer Science

File Handling in Python (Binary & CSV Files): Class 12 CBSE Previous-Year Questions with Answers

CBSE Question Papers →

20 questions from CBSE board, sample and compartment papers (2024–2027), 1 to 5 marks, each with a model answer. Questions on binary file modes, pickle dump() and load(), adding, searching and updating records in a binary file, and CSV files with csv.writer, writerow(), writerows() and csv.reader. Programming answers open in the Python Lab.

Showing 20 of 295 questions
Group

File Handling in Python (Binary & CSV file)

20 Questions
File Handling in Python (Binary & CSV file)· 1 Mark· Q17· 2024 Board Paper
Assertion (A): CSV file is a human readable text file where each line has a number of fields, separated by comma or some other delimiter. Reason (R): writerow() method is used to write a single row in a CSV file. Mark the correct choice as:
  1. (A)Both (A) and (R) are true and (R) is the correct explanation for (A).
  2. (B)Both (A) and (R) are true and (R) is not the correct explanation for (A).
  3. (C)(A) is true but (R) is false.
  4. (D)(A) is false but (R) is true.
File Handling in Python (Binary & CSV file)· 4 Marks· Q32· 2024 Board Paper Python
Sangeeta is a Python programmer working in a computer hardware company. She has to maintain the records of the peripheral devices. She created a csv file named Peripheral.csv, to store the details. The structure of Peripheral.csv is: [P_id, P_name, Price] where P_id is Peripheral device ID (integer), P_name is Peripheral device name (String), Price is Peripheral device price (integer). Sangeeta wants to write the following user defined functions: Add_Device() : to accept a record from the user and add it to a csv file, Peripheral.csv. Count_Device() : To count and display number of peripheral devices whose price is less than 1000.
File Handling in Python (Binary & CSV file)· 5 Marks· Q34· 2024 Board Paper Python
(A) (i) Differentiate between 'w' and 'a' file modes in Python. (ii) Consider a binary file, items.dat, containing records stored in the given format: {item_id: [item_name, amount]} Write a function, Copy_new(), that copies all records whose amount is greater than 1000 from items.dat to new_items.dat. OR (B) (i) What is the advantage of using with clause while opening a data file in Python? Also give syntax of with clause. (ii) A binary file, EMP.DAT has the following structure: [Emp_Id, Name, Salary] Write a user defined function, disp_Detail(), that would read the contents of the file EMP.DAT and display the details of those employees whose salary is below 25000.
File Handling in Python (Binary & CSV file)· 1 Mark· Q20· 2025 Board Paper
Assertion (A): For a binary file opened using 'rb' mode, the pickle.dump() method will display an error. Reason (R): The pickle.dump() method is used to read from a binary file.
  1. (A)Both Assertion (A) and Reason (R) are true and Reason (R) is the correct explanation for Assertion (A).
  2. (B)Both Assertion (A) and Reason (R) are true and Reason (R) is not the correct explanation for Assertion (A).
  3. (C)Assertion (A) is true but, Reason (R) is false.
  4. (D)Assertion (A) is false but, Reason (R) is true.
File Handling in Python (Binary & CSV file)· 4 Marks· Q33· 2025 Board Paper Python
A csv file "P_record.csv" contains the records of patients in a hospital. Each record of the file contains the following data: - Name of a patient - Disease - Number of days patient is admitted - Amount For example, a sample record of the file may be: ["Gunjan","Jaundice",4,15000] Write the following Python functions to perform the specified operations on this file: (i) Write a function read_data() which reads all the data from the file and displays the details of all the 'Cancer' patients. (ii) Write a function count_rec() which counts and returns the number of records in the file.
File Handling in Python (Binary & CSV file)· 5 Marks· Q36· 2025 Board Paper Python
A file, PASSENGERS.DAT, stores the records of passengers using the following structure: [PNR, PName, BRDSTN, DESTN, FARE] where: PNR - Passenger Number (string type) PName - Passenger Name (string type) BRDSTN - Boarding Station Name (string type) DESTN - Destination Station Name (string type) FARE - Fare amount for the journey (float type) Write user defined functions in Python for the following tasks: (i) Create() - to input data for passengers and write it in the binary file PASSENGERS.DAT. (ii) SearchDestn(D) - to read contents from the file PASSENGERS.DAT and display the details of those Passengers whose DESTN matches with the value of D. (iii) UpdateFare() - to increase the fare of all passengers by 5% and rewrite the updated records into the file PASSENGERS.DAT.
File Handling in Python (Binary & CSV file)· 1 Mark· Q10· 2026 Board Paper
Consider the statement given below: f1 = open("pqr.dat", "_______") Which of the following is the correct file mode to open the file in read only mode?
  1. (A)a
  2. (B)rb
  3. (C)r+
  4. (D)rb+
File Handling in Python (Binary & CSV file)· 4 Marks· Q33· 2026 Board Paper Python
A csv file "States.csv" contains [State, Capital, Population, Language]. Write a Python program which reads data from this file and appends records where population is more than 10000000 into another csv file 'More.csv'. Note: Skip the Header row.
File Handling in Python (Binary & CSV file)· 5 Marks· Q36· 2026 Board Paper Python
Binary file 'RESOURCES.DAT' stores (R_ID, R_Name, R_Expertise, Charges). Write: (i) Append() to input data and write to file. (ii) Update() to increase Charges of each resource person by 500.
File Handling in Python (Binary & CSV file)· 4 Marks· Q33· 2025 Compartment Paper Python
Write Python code for a CSV file 'CLUB.CSV' [Mno, Name, Mobile, Fee]: NewMembers(): accept records from user and add to file. PriorityMember(): find and display members paying Fee > 35000.
File Handling in Python (Binary & CSV file)· 5 Marks· Q36· 2025 Compartment Paper Python
Records of doctors are stored in a binary file 'DOCTORS.DAT' [D_ID, D_Name, D_Dept, Experience]. (I) Write one difference between binary and CSV files. (II) Write a Python function to read and display all records. (III) Write a Python function to add a doctor.
File Handling in Python (Binary & CSV file)· 4 Marks· Q32· 2024 Sample Paper Python
Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has created a csv file named Result.csv, to store the results of students in different sports events. The structure of Result.csv is : [St_Id, St_Name, Game_Name, Result] Where St_Id is Student ID (integer) ST_name is Student Name (string) Game_Name is name of game in which student is participating(string) Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie' For efficiently maintaining data of the event, Vedansh wants to write the following user defined functions: Accept() – to accept a record from the user and add it to the file Result.csv. The column headings should also be added on top of the csv file. wonCount() – to count the number of students who have won any event. As a Python expert, help him complete the task.
File Handling in Python (Binary & CSV file)· 5 Marks· Q34· 2024 Sample Paper Python
(i) Differentiate between r+ and w+ file modes in Python. (ii) Consider a file, SPORT.DAT, containing records of the following structure: [SportName, TeamName, No_Players] Write a function, copyData(), that reads contents from the file SPORT.DAT and copies the records with Sport name as “Basket Ball” to the file named BASKET.DAT. The function should return the total number of records copied to the file BASKET.DAT. OR (i) How are text files different from binary files? (ii) A Binary file, CINEMA.DAT has the following structure: {MNO:[MNAME, MTYPE]} Where MNO – Movie Number MNAME – Movie Name MTYPE is Movie Type Write a user defined function, findType(mtype), that accepts mtype as parameter and displays all the records from the binary file CINEMA.DAT, that have the value of Movie Type as mtype.
File Handling in Python (Binary & CSV file)· 4 Marks· Q33· 2025 Sample Paper
A csv file "Happiness.csv" contains the data of a survey. Each record of the file contains the following data: ● Name of a country ● Population of the country ● Sample Size (Number of persons who participated in the survey in that country) ● Happy (Number of persons who accepted that they were Happy) For example, a sample record of the file may be: ['Signiland', 5673000, 5000, 3426] Write the following Python functions to perform the specified operations on this file: (I) Read all the data from the file in the form of a list and display all those records for which the population is more than 5000000. (II) Count the number of records in the file.
File Handling in Python (Binary & CSV file)· 5 Marks· Q36· 2025 Sample Paper
Surya is a manager working in a recruitment agency. He needs to manage the records of various candidates. For this, he wants the following information of each candidate to be stored: - Candidate_ID - integer - Candidate_Name - string - Designation - string - Experience - float You, as a programmer of the company, have been assigned to do this job for Surya. (I) Write a function to input the data of a candidate and append it in a binary file. (II) Write a function to update the data of candidates whose experience is more than 10 years and change their designation to "Senior Manager". (III) Write a function to read the data from the binary file and display the data of all those candidates who are not "Senior Manager".
File Handling in Python (Binary & CSV file)· 4 Marks· Q33· 2026 Sample Paper Python
Raj is the manager of a medical store. To keep track of sales records, he has created a CSV file named Sales.csv, which stores the details of each sale. The columns of the CSV file are: Product_ID, Product_Name, Quantity_Sold and Price_Per_Unit. Help him to efficiently maintain the data by creating the following user-defined functions: I. Accept() – to accept a sales record from the user and add it to the file Sales.csv. II. CalculateTotalSales() – to calculate and return the total sales based on the Quantity_Sold and Price_Per_Unit.
File Handling in Python (Binary & CSV file)· 5 Marks· Q36· 2026 Sample Paper Python
Mr. Ravi, a manager at a tech company, needs to maintain records of employees. Each record should include: Employee_ID, Employee_Name, Department and Salary. Write the Python functions to: I. Input employee data and append it to a binary file. II. Update the salary of employees in the "IT" department to 200000.
File Handling in Python (Binary & CSV file)· 1 Mark· Q20· 2027 Sample Paper
Assertion (A): Opening a file in mode rb allows you to read a binary file without modifying it. Reason (R): In rb mode, if the file does not exist, Python creates a new empty file.
  1. (A)Both A and R are True and R is the correct explanation for A.
  2. (B)Both A and R are True and R is not the correct explanation for A.
  3. (C)A is True but R is False.
  4. (D)A is False but R is True.
File Handling in Python (Binary & CSV file)· 4 Marks· Q33· 2027 Sample Paper
Kabir runs a vehicle rental agency and maintains a CSV file named Rentals.csv. The file stores daily rental details with columns: Vehicle_ID, Vehicle_Type, Hours_Rented, Rate_Per_Hour Create the following user-defined functions: I. AddRental() – to accept rental details from the user and append them to Rentals.csv. II. CalculateTotalRent() – to calculate and return the total rent earned (Hours_Rented × Rate_Per_Hour).
File Handling in Python (Binary & CSV file)· 5 Marks· Q36· 2027 Sample Paper
A college administrator, Atul, maintains student course enrollment records. Each record includes Roll_No, Student_Name, Course, Fees. Write Python functions to: I. Input enrollment details and append them to a binary file. II. Update the Fees of all students enrolled in the "Data Science" course to 75000.