Skip to main content
Chapter 2 of 13
NCERT Solutions

File Handling in Python — NCERT Solutions

CBSE · Class 12 · Computer Science

NCERT question answers for File Handling in Python, CBSE Class 12 Computer Science: 20 textbook questions solved step by step. Covers Exercise.

44 questions96 flashcards5 formulas & key relations5 concepts

Interactive on Super Tutor

Studying File Handling in Python? Get the full interactive chapter.

Quizzes, flashcards, AI doubt-solver and a step-by-step study plan — built for NCERT solutions and more.

Free trial, no card needed.

Illustrates why file handling is necessary, showing the difference between temporary data in RAM (variables) and permanent data storage on secondary devices (files).
Super Tutor

One of 9 illustrations for File Handling in Python in Super Tutor — alongside flashcards, concept maps and practice questions.

See them all
20 Questions Solved · 1 Section

The first 10 solutions are open to read. The other 10 are free with a Super Tutor account. Unlock all 20 free in Super Tutor

Exercise

1(a)text file and binary fileShow solution

Text file vs binary file:

  • A text file consists of readable characters such as alphabets, numbers, and symbols. Its contents are stored as ASCII/Unicode byte values and can be opened in a text editor.
  • A binary file stores data as bytes that do not represent readable characters. It may contain images, audio, video, executable files, etc., and needs specific software to read or write it.

Not sure why a step works? check your working in Super Tutor

1(b)readline() and readlines()Show solution
  • readline() reads one complete line from a file. If a number is given, it reads up to that many bytes, but not beyond the newline character.
  • readlines() reads all lines from a file and returns them as a list of strings.

Not sure why a step works? check your working in Super Tutor

1(c)write() and writelines()Show solution
  • write() writes a single string to a text file and returns the number of characters written.
  • writelines() writes multiple strings from an iterable like a list or tuple, and it does not return the number of characters written.

Not sure why a step works? check your working in Super Tutor

2(a)open()Show solution

The open() function is used to open a file in Python. It returns a file object/file handle that can be used for reading or writing.

Syntax:

file_object = open(file_name, access_mode)

Not sure why a step works? check your working in Super Tutor

2(b)read()Show solution

The read() method is used to read a specified number of bytes from a file.

Syntax:

file_object.read(n)

If no argument or a negative value is given, it reads the entire file.

Not sure why a step works? check your working in Super Tutor

2(c)seek()Show solution

The seek() method is used to move the file object to a particular position in the file.

Syntax:

file_object.seek(offset[, reference_point])
  • offset = number of bytes to move
  • reference_point = starting position: 0 beginning, 1 current position, 2 end of file

Not sure why a step works? check your working in Super Tutor

2(d)dump()Show solution

The dump() method of the pickle module is used to pickle a Python object and write it to a binary file.

Syntax:

dump(data_object, file_object)

Not sure why a step works? check your working in Super Tutor

3(a)a text file “example.txt” in both read and write modeShow solution

A text file opened in both read and write mode uses the mode r+.

Statement:

file_object = open("example.txt", "r+")

Not sure why a step works? check your working in Super Tutor

3(b)a binary file “bfile.dat” in write modeShow solution

A binary file opened in write mode uses the mode wb.

Statement:

file_object = open("bfile.dat", "wb")

Not sure why a step works? check your working in Super Tutor

3(c)a text file “try.txt” in append and read modeShow solution

A text file opened in append and read mode uses the mode a+.

Statement:

file_object = open("try.txt", "a+")

Not sure why a step works? check your working in Super Tutor

3(d)a binary file “btry.dat” in read only mode.

Free with a Super Tutor account

4Why is it advised to close a file after we are done with the read and write operations? What will happen if we do not close it? Will some error message be flashed?

Free with a Super Tutor account

5What is the difference between the following set of statements (a) and (b):

Free with a Super Tutor account

5(a)P = open(“practice.txt”, “r”)
P.read(10)

Free with a Super Tutor account

5(b)with open(“practice.txt”, “r”) as P:
x = P.read()

Free with a Super Tutor account

6Write a command(s) to write the following lines to the text file named hello.txt. Assume that the file is opened in append mode.

“Welcome my class”
“It is a fun place”
“You will learn and play”

Free with a Super Tutor account

7Write a Python program to open the file hello.txt used in question no 6 in read mode to display its contents. What will be the difference if the file was opened in write mode instead of append mode?

Free with a Super Tutor account

8Write a program to accept string/sentences from the user till the user enters “END” to. Save the data in a text file and then display only those sentences which begin with an uppercase alphabet.

Free with a Super Tutor account

9Define pickling in Python. Explain serialization and deserialization of Python object.

Free with a Super Tutor account

10Write a program to enter the following records in a binary file:
Item Nointeger
Item_Namestring
Qtyinteger
Pricefloat

Number of records to be entered should be accepted from the user. Read the file to display the records in the following format:

Item No:
Item Name :
Quantity:
Price per item:
Amount:( to be calculated as Price * Qty)

Free with a Super Tutor account

10 more solved questions in File Handling in Python

They are free with a Super Tutor account, along with practice quizzes and flashcards for this chapter. Free to start, no card needed.

Frequently Asked Questions

What are the important topics in File Handling in Python for CBSE Class 12 Computer Science?
Key topics in File Handling in Python include Basics of Files and Types of Files, Opening and Closing Files, File Access Modes, Writing to Text Files. Study these first, then practise questions on each for the CBSE Class 12 board exam.
Are these NCERT Solutions for File Handling in Python free?
The first 10 of the 20 solutions on this page are open to read. The other 10 are free with a Super Tutor account — signing up is free and needs no card.
How should I revise File Handling in Python for the CBSE Class 12 board exam?
Learn the core ideas first, then work through the 44 practice questions on File Handling in Python. Revise definitions regularly and use flashcards for quick recall before the exam.

Sources & Official References

Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.

For serious students

Get the full File Handling in Python chapter — start free.

Quizzes, flashcards, an AI doubt solver and a study plan for CBSE Class 12 Computer Science. Free to start, no card needed.