Skip to content
← Computer Science

Computer Science · Class 12 Optional

Ch 2File Handling in Python — Class 12 Computer Science, concept-first.

A program’s variables exist only while the program runs. Once execution ends, the data stored in those variables is lost. This means every time you run the program, you must re-enter the input, and any output produced during the previous run is gone.

22

Q&A

8

Concepts

Not available

Exam weightage

Start learning — read this chapter →

Key concepts

Hover a concept to preview it and jump to its most relevant Q&A.

File Size Comparison

Think about the last time you tried to email a document or upload a photo to a website. If the file was too large, the website probably stopped you or the email refused to send.

Start with this concept →

Chapter contents

The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.

2.1

Introduction to Files

A program’s variables exist only while the program runs. Once execution ends, the data stored in those variables is lost.

2.2

Types of Files

Every file on a computer — whether it is a text document, an image, a video, or a program — is stored as a collection of 0s and 1s. That is the only language a computer understands.

2.2.1

Text file

A text file is simply a sequence of characters — alphabets, digits, and special symbols — stored permanently on a disk. Common examples include files with extensions like .txt, .py, and .csv.

2.2.2

Binary Files

Binary files store data as a sequence of bytes (0s and 1s), just like text files. The critical difference is what those bytes represent.

2.3

Opening and Closing a Text File

In real-world applications, programs work with data arriving from many different sources — databases, CSV files, HTML, XML, JSON feeds, and more.

2.3.1

Opening a file

To work with a file in Python, the first step is to open it. You do this using the open() function, which returns a file object (also called a file handle).

2.3.2

Closing a file

Once a file has been opened and you have finished reading from it or writing to it, you must close it.

2.3.3

Opening a file using with clause

The with clause in Python provides a cleaner and safer way to work with files. Its real power is that it handles file closing automatically — you never have to remember to call close().

2.4

Writing to a Text File

To write data into a file, you must first open it in either write mode ('w') or append mode ('a'). If you open an existing file in write mode, its previous contents are erased and the file object is p…

2.4.1

The write() method

The write() method is how you actually put data into a text file. It does not create a file — that is the job of the open() function with the 'w' mode.

2.4.2

The writelines() method

2 Q

The writelines() method is designed for writing multiple strings to a file in one go. Instead of calling write() repeatedly for each string, you pass an iterable object — like a list or a tuple — that…

2.5

Reading from a Text File

Before you can read from a file, you must make sure it is opened in a mode that permits reading — "r", "r+", "w+", or "a+".

2.5.1

The read() method

The read() method is how you pull data out of a file and into your Python program. It reads a specified number of bytes (characters) from the file, starting from the current position of the file point…

2.5.2

The readline([n]) method

The readline() method reads exactly one line from a file. A "line" in a text file is any sequence of characters that ends with a newline character (n).

2.5.3

The readlines() method

The readlines() method reads all the lines of a file at once and returns them as a list of strings. Each string in the list corresponds to one line from the file, and crucially, each string includes t…

2.6

Setting Offsets in a File

The functions you have learnt so far — read(), readline(), readlines(), write(), writelines() — all access data sequentially: the file pointer moves forward as you read or write, and you cannot easily…

2.6.1

The tell() method

The tell() method answers a simple question: "Where exactly am I in this file right now?" When you read from or write to a file, the file object keeps track of its current position — like a cursor tha…

2.6.2

The seek() method

The seek() method lets you move the file object to a specific byte position inside a file. This is essential when you need to read from or write to a particular location without going through the enti…

2.7

Creating and Traversing a Text File

Having learnt how to open and close a file, read and write data in a text file, and find or move the file object's position, let's now put these skills together to perform two basic operations on a te…

2.7.1

Creating a file and writing data

To create a file in Python, you use the open() method. This method takes two essential arguments: the filename and the mode in which you want to open it.

2.7.2

Traversing a file and displaying data

To read data back from a text file, you must open it in read mode ("r"). Once opened, reading begins from the very first byte of the file — the file pointer is at position 0.

2.8

The Pickle Module

Python treats everything as an object — lists, tuples, dictionaries, and even custom data structures are all objects.

2.8.1

The dump() method

The dump() method is how you write Python objects into a binary file — a process called pickling. Pickling converts a Python object (like a list, dictionary, or number) into a byte stream that can be…

2.8.2

The load() method

The load() method is the counterpart of dump() — it brings data back from a binary file into a Python object. This process is called unpickling.

2.8.3

File handling using pickle module

When you write data to a text file, everything becomes a string. Numbers, lists, dictionaries — all get converted to text.

Summary

- A file is a named location on a secondary storage media where data are permanently stored for later access.

Exercises

Sample & Board Papers

Sample papers and previous-year board questions for this subject.