The following Python function is designed to remove the records of students…

2026

The following Python function is designed to remove the records of students who scored less than 33 marks from a CSV file named result.csv. The file contains data in the format RollNo, Marks. To achieve this, the function reads all valid records into a list and then overwrites the original file with only those students who have passed (Marks ≥ 33).

import csv

def delete_failing_students():
    passed_students = []
    try:
        with open( ______ (A) ______ ) as File:
            reader_obj = ______ (B) ______
            for row in reader_obj:
                if int(row[1]) >= 33:
                    passed_students.append(row)

            File.seek(0)
            Writer_obj = ______ (C) ______
            Writer_obj. ______ (D) ______
            File.truncate()

    except FileNotFoundError:
        print("File not found.")

Questions:

(A) Write the arguments including newline inside the open() function for Blank A to open the CSV file for reading as well as writing purposes.

(B) Write the method call for Blank B to create an object that can read the lines of the CSV file.

(C) Provide the method call for Blank C to create an object used to write data into the CSV file.

(D) Provide the method call for Blank D to write the content of the Python list passed_students into the CSV file.

Attempted by 59 students.

Show answer & explanation

(A) Open statement with arguments:
"result.csv", "r+", newline=''
Reason: "r+" allows both reading and writing, and newline='' ensures proper handling of CSV rows.

(B) Method to read CSV file:
csv.reader(File)
Reason: Creates a reader object to read rows from the CSV file.

(C) Method to create writer object:
csv.writer(File)
Reason: Creates a writer object to write data into the CSV file.

(D) Method to write list data:
writerows(passed_students)
Reason: Writes all rows from the list passed_students into the CSV file.

Explore the full course: Hpsc Pgt Computer Science