Skip to main content
Chapter 2 of 13
NCERT Solutions

File Handling in Python

CBSE · Class 12 · Computer Science

NCERT Solutions for File Handling in Python — CBSE Class 12 Computer Science.

44 questions96 flashcards5 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.

1,000+ Class 12 students started this chapter today

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

Super Tutor has 9+ illustrations like this for File Handling in Python alone — flashcards, concept maps, and step-by-step visuals.

See them all
21 Questions Solved · 1 Section

11 worked solutions below. Unlock all 21 free in Super Tutor

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.

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

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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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.
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?
5What is the difference between the following set of statements (a) and (b):
5(a)P = open(“practice.txt”, “r”)
P.read(10)
5(b)with open(“practice.txt”, “r”) as P:
x = P.read()
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.
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?
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.
9Define pickling in Python. Explain serialization and deserialization of Python object.
10Write a program to enter the following records in a binary file:

10 more solved questions in File Handling in Python

Every remaining exercise is solved step by step in Super Tutor, plus practice quizzes and flashcards for this chapter. Free to start.

Stuck on a step?

Ask Super Tutor AI to explain any solution on this page in a simpler way — free, 24x7.

Ask a Doubt Free

Frequently Asked Questions

What are the important topics in File Handling in Python for CBSE Class 12 Computer Science?
File Handling in Python covers several key topics that are frequently asked in CBSE Class 12 board exams. Focus on the core concepts listed on this page and practise related questions to build confidence.
How to score full marks in File Handling in Python — CBSE Class 12 Computer Science?
Understand the core concepts first, then work through the 44 practice questions available for this chapter. Revise formulas and definitions regularly, and use flashcards for quick recall before the exam.
Where can I get free NCERT Solutions for File Handling in Python Class 12 Computer Science?
This page has free step-by-step NCERT Solutions for every exercise question in File Handling in Python (CBSE Class 12 Computer Science) — written the way examiners award marks: given, formula, working, answer.

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 — for free.

Quizzes, flashcards, AI doubt-solver and a step-by-step study plan for CBSE Class 12 Computer Science.