School Revise · Class 11 Computer Science · Chapter 2
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.
|
|
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.
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 |
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.
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.
Explore the idea by tapping. The interactive opens right here in the lesson.
Write and run real Python right here in the lesson, then work through the practice problems with answers.
Type each one into the code lab above, then open the card to see the worked solution and its output.
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
w (write mode).
a (append mode).
read().
It opens the file and closes it automatically.
|
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.