Computer Science · Python File Handling
Case Study: A teacher wants a simple Python script to analyze daily attendance from a CSV file named 'attendance.csv'. Each row has two columns: 'roll_no' and 'status' ('P' for present, 'A' for absent). Question: Which of the following is the recommended and safest way to open and read the CSV file in Python to ensure it closes automatically even if an exception occurs?
- 1f = open('attendance.csv'); data = f.read()
- 2with open('attendance.csv', 'r') as f: data = f.read()✓
- 3open('attendance.csv', 'r').close()
- 4data = read('attendance.csv')
Correct answer
with open('attendance.csv', 'r') as f: data = f.read()
Download Single Question PDF
Save this question and verified answer as a clean study sheet with live clickable link.
Share with Friends & Study Groups
Challenge your peer aspirants on WhatsApp or Telegram to solve this question!
Preparing for Computer Science?
Practice full-length mock tests with real exam timers, instant score analysis, and all-India ranking.
Want more practice questions on Python File Handling?
Solve complete topic-wise tests with detailed solutions on GkGet.
Related Practice Questions
Recommended practice questions in Computer Science · Python File Handling