Curriculum
Course: Grade XII Computer Science
Login
Text lesson

Ch-2 File Handling in Python

School Revise · Class 11 Computer Science · Chapter 2

File Handling in Python

Code 083, Class 12. Files let a program store data permanently. Here we open, read, write and append text files, and meet the CSV file.

r w a

file modes

with

auto close

What this chapter is about

A file keeps data even after the program ends. We open a file in a mode, read or write data, then close it. Files can be text, binary or CSV.

1. Opening, modes and closing

Use open(name, mode). The modes are r to read, w to write (it replaces the file), a to append (add to the end), and r+ to read and write. Always close() the file when done.

f = open(“data.txt”, “w”) f.write(“Hello, School Revise”) f.close() Output: file is written

2. Reading a file

read() gives the whole file, readline() gives one line, and readlines() gives a list of lines. A for loop reads a file line by line.

3. The with statement and CSV

The with statement opens a file and closes it for you automatically, which is safer. A CSV file stores rows of values separated by commas, handled by the csv module.

Practise with the interactive

Explore the idea by tapping. The interactive opens right here in the lesson.

Try it in the code lab

Write and run real Python right here in the lesson, then work through the practice problems with answers.

Starting Python...

Coding practice problems, with answers

Type each one into the code lab above, then open the card to see the worked solution and its output.

Problem 1. Write text to a file.

SOLUTION

f = open(“data.txt”, “w”) f.write(“Hello, School Revise”) f.close() print(“written”)

OUTPUT

written
Problem 2. Read a whole file.

SOLUTION

f = open(“data.txt”, “r”) print(f.read()) f.close()

OUTPUT

Hello, School Revise
Problem 3. Read a file line by line.

SOLUTION

f = open(“data.txt”, “w”) f.write(“line1\nline2\n”) f.close() f = open(“data.txt”, “r”) for line in f: print(line.strip()) f.close()

OUTPUT

line1 line2
Problem 4. Append to a file.

SOLUTION

f = open(“data.txt”, “a”) f.write(“more text”) f.close() print(“appended”)

OUTPUT

appended
Problem 5. Use with to open a file.

SOLUTION

with open(“data.txt”, “w”) as f: f.write(“safe write”) print(“done”)

OUTPUT

done
Problem 6. Count the lines in a file.

SOLUTION

with open(“data.txt”, “w”) as f: f.write(“a\nb\nc\n”) with open(“data.txt”) as f: print(len(f.readlines()))

OUTPUT

3
Problem 7. Write several lines with writelines.

SOLUTION

lines = [“one\n”, “two\n”] with open(“data.txt”, “w”) as f: f.writelines(lines) print(“written”)

OUTPUT

written
Problem 8. Read only the first line.

SOLUTION

with open(“data.txt”, “w”) as f: f.write(“first\nsecond\n”) with open(“data.txt”) as f: print(f.readline().strip())

OUTPUT

first
Problem 9. Find the file position with tell.

SOLUTION

with open(“data.txt”, “w”) as f: f.write(“abc”) with open(“data.txt”) as f: f.read(2) print(f.tell())

OUTPUT

2
Problem 10. Write and read a CSV row.

SOLUTION

import csv with open(“marks.csv”, “w”, newline=””) as f: csv.writer(f).writerow([“Ravi”, 82]) with open(“marks.csv”) as f: print(f.read().strip())

OUTPUT

Ravi,82

Practice set A, multiple choice

1. Which mode replaces the whole file?

w (write mode).

2. Which mode adds to the end of a file?

a (append mode).

3. Which method reads the entire file at once?

read().

4. What does the with statement do for a file?

It opens the file and closes it automatically.

Quick summary

A file stores data permanently. open(name, mode) opens it: r reads, w writes and replaces, a appends, r+ reads and writes. read(), readline() and readlines() read data; write() and writelines() write it; close() closes it. The with statement closes the file for you, and the csv module handles comma separated files.

Open the Virtual Lab

These free Class 12 Computer Science notes explain file handling in Python, opening files and modes, reading and writing text files, the with statement, and CSV files with worked Python programs and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.

© 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 12 Computer Science syllabus. Unauthorised copying is not permitted.

Layer 1
Login Categories