Computer ScienceClass 12File Handling in Python

File Handling in Python: Complete Guide for Class 12 NCERT Students

By ConceptScroll Team · Published on 2 July 2026 · 5 min read

File Handling in Python: Complete Guide for Class 12 NCERT Students

File Handling in Python is a crucial topic in Class 12 NCERT Computer Science. It teaches students how to open, read, write, and close files efficiently, enabling programs to store and retrieve data from secondary storage.

Understanding File Handling in Python for Class 12

File Handling in Python allows programs to interact with files stored on your computer or other storage devices. In Class 12 NCERT Computer Science, this concept is essential to learn how data can be saved permanently and accessed later.

A file is a collection of bytes stored on a device like a hard disk or USB drive. Python uses file objects as handles to work with these files. The primary function to open a file is:

``python file_object = open(file_name, access_mode) ``

Here, file_name is the name or path of the file, and access_mode determines how the file will be used (read, write, append, etc.). Once opened, you can perform various operations like reading or writing data. After operations, closing the file is important to free resources and save changes.

File Modes in Python: How to Open Files Correctly

File modes define what you can do with a file once it is opened. The NCERT Class 12 syllabus explains these modes clearly. Here is a summary:

File ModeDescriptionFile Pointer Position
rRead-only mode. File must exist.Start of file
rbRead binary mode. For non-text files.Start of file
r+Read and write mode. File must exist.Start of file
wWrite mode. Overwrites existing file or creates new.Start of file
wb+Write and read in binary. Overwrites or creates new.Start of file
aAppend mode. Adds data at end or creates new file.End of file
a+Append and read mode. Creates new if not exists.End of file

Choosing the correct mode is important for your program's behaviour. For example, using w will erase existing content, while a adds new data without deleting old content.

Want to test yourself on File Handling in Python? Try our free quiz →

Opening and Closing Files: Best Practices in Python

In Python, opening a file returns a file object that you use to read or write data. After completing your operations, always close the file to avoid data loss or resource leaks.

Example of opening and closing a file:

``python f = open('data.txt', 'r') # Open file in read mode content = f.read() # Read content f.close() # Close file ``

However, a better and safer way is to use the with statement. It automatically closes the file even if an error occurs:

``python with open('data.txt', 'r') as f: content = f.read() # File is automatically closed here ``

This method is recommended in NCERT Class 12 to ensure clean and error-free file handling.

Reading and Writing Files: Methods and Examples

Once a file is opened, you can read from or write to it using specific methods.

Reading Methods

  • read(size) reads a specified number of characters
  • readline() reads one line at a time
  • readlines() reads all lines into a list

Example:

``python with open('story.txt', 'r') as f: line = f.readline() print(line) ``

Writing Methods

  • write(string) writes a string to the file
  • writelines(list_of_strings) writes multiple lines

Example:

``python with open('output.txt', 'w') as f: f.write('Hello, Class 12 students!') ``

Remember, writing in w mode overwrites existing content, while a mode appends data.

Binary vs Text File Modes: When to Use Which

Python supports two main file modes: text and binary.

  • Text mode (default) reads and writes strings. It handles encoding and decoding automatically.
  • Binary mode reads and writes bytes, useful for images, audio, and other non-text files.

To open a file in binary mode, add b to the mode string:

  • 'rb' for reading binary
  • 'wb' for writing binary

Example:

``python with open('image.jpg', 'rb') as img: data = img.read() ``

Use binary mode when working with files that are not plain text to avoid data corruption.

File Pointer and File Offset: Understanding Positions in Files

When working with files, the file pointer indicates the current position where reading or writing will occur. It moves automatically as you read or write data.

  • At file open, pointer is at the beginning (0) for modes like r or w.
  • In append mode (a), pointer starts at the end.

Python provides methods to manage the file pointer:

  • tell() returns the current position
  • seek(offset, whence) moves the pointer to a new position

Example:

``python with open('data.txt', 'r') as f: print(f.tell()) # Prints 0 f.read(10) print(f.tell()) # Prints 10 f.seek(0) # Moves pointer back to start ``

Understanding file pointers helps in precise file manipulation, a key topic in Class 12 NCERT.

Frequently asked questions

What is the purpose of the open() function in Python file handling?

The open() function opens a file and returns a file object used to perform read/write operations.

How do you open a file in append mode in Python?

Use open('filename', 'a') to open a file in append mode, which adds data at the file's end.

What does the 'r+' mode do when opening a file?

'r+' mode opens a file for both reading and writing. The file must exist beforehand.

Why should files be closed after operations in Python?

Closing files frees system resources and ensures all data is saved properly to the file.

What is the advantage of using the with statement for file handling?

The with statement automatically closes the file after the block, even if errors occur.

How can you find the current position of the file pointer?

Use the tell() method on the file object to get the current file pointer position.

Ready to ace this chapter?

Get the full File Handling in Python chapter — interactive notes, diagrams, worked solutions, polls and a free practice quiz — in the ConceptScroll app.

Open in ConceptScroll →

Study smarter with ConceptScroll

Daily NCERT-aligned reels, AI doubt solving and chapter quizzes — all free.

Start learning free
#class 12#computer science#file handling#file modes#file operations#ncert#programming#python#python files

Continue reading