Skip to main content
Chapter 2 of 13
NCERT Solutions

File Handling in Python — NCERT Solutions

Karnataka Board · Class 12 · Computer Science

NCERT Solutions for File Handling in Python, Karnataka Board Class 12 Computer Science: 21 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.

21 Questions Solved · 1 Section

The first 11 solutions are open to read. The other 10 are free with a Super Tutor account.

Exercise

1Differentiate between:Show solution
  • Text file: contains human-readable characters; can be opened by any text editor.
  • Binary file: contains non-human-readable data in bytes; needs specific programs to access it.
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.
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.
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.
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)
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.

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
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)
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+")
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")
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+")
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.

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:

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 Karnataka Board 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 Karnataka Board Class 12 board exam.
Are these NCERT Solutions for File Handling in Python free?
The first 11 of the 21 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 Karnataka Board 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 Karnataka Board Class 12 Computer Science. Free to start, no card needed.