Computer ScienceClass 12File Handling in Python

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

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

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

File Handling in Python is a vital topic in Class 12 NCERT Computer Science. It teaches you how to create, read, write, and manage files, enabling your programs to store and retrieve data effectively.

Understanding File Types: Text vs Binary Files

Files in computers are sequences of bytes storing data in two main types:

  • Text Files: These contain human-readable characters such as alphabets, digits, and symbols. Examples include .txt, .py, and .csv files. When opened in editors like Notepad, their content is readable.
  • Binary Files: These store data in bytes not corresponding to readable characters, such as images, audio, videos, or executable files. Opening binary files in text editors shows unreadable characters.

Each character in a text file is stored using ASCII or Unicode values. For example, the letter 'A' is stored as ASCII value 65. Text files use special characters like newline \n to mark the end of lines.

Understanding these file types helps you choose the right mode and method when handling files in Python.

File Modes in Python: How to Open Files Correctly

Python provides various modes to open files depending on the operation:

File ModeDescriptionFile Pointer Position
rRead-only mode. File must exist.Start of file
rbRead-only binary mode.Start of file
r+ or +rRead and write mode. File must exist.Start of file
wWrite mode. Overwrites existing file or creates new.Start of file
wb+ or +wbRead, write, binary mode. Overwrites or creates new.Start of file
aAppend mode. Adds data at end or creates new file.End of file
a+ or +aAppend and read mode. Creates file if not exists.End of file

Example:

To open a file named story.txt in write mode:

``python f = open(r"D:\TEMP\story.txt", "w") ``

Choosing the correct mode is essential to avoid data loss or errors.

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

Basic File Operations: Reading and Writing in Python

Once a file is opened, Python allows you to perform operations like reading and writing.

  • Reading Files: Use read(), readline(), or readlines() methods.

``python f = open("file.txt", "r") content = f.read() # Reads entire file f.close() ``

  • Writing Files: Use the write() method to add content.

``python f = open("file.txt", "w") f.write("Hello, Class 12 students!") f.close() ``

  • Appending Files: Use append mode a to add data without deleting existing content.

``python f = open("file.txt", "a") f.write(" More data appended.") f.close() ``

Always close files after operations to free resources and save data properly.

Working with File Pointers and Positioning

When you open a file, a file pointer indicates the current position for reading or writing.

  • The pointer starts at the beginning in modes like r and w.
  • In append mode a, it starts at the end.

Python provides methods to manage the pointer:

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

Example:

``python f = open("file.txt", "r") print(f.tell()) # Prints 0 at start f.read(5) # Reads first 5 characters print(f.tell()) # Prints 5, pointer moved f.seek(0) # Moves pointer back to start f.close() ``

Managing the file pointer is useful for reading or writing at specific positions.

Comparison of File Handling Modes in Python

Here is a quick comparison of common file modes to help you choose the right one:

ModePurposeOverwrites File?File Pointer Start
rRead-onlyNoBeginning
wWrite-onlyYesBeginning
aAppend-onlyNoEnd
r+Read and writeNoBeginning
a+Append and readNoEnd
rbRead binaryNoBeginning
wb+Read, write binaryYesBeginning

Use this table to avoid accidental data loss and to perform correct file operations.

Worked Example: Creating and Reading a Text File in Python

Let's create a simple text file, write data, and then read it.

```python # Create and write to a file f = open("student.txt", "w") f.write("Name: Rahul\nClass: 12\nSubject: Computer Science") f.close()

# Read the file content f = open("student.txt", "r") content = f.read() print(content) f.close() ```

Output: `` Name: Rahul Class: 12 Subject: Computer Science ``

This example demonstrates basic file handling operations covered in Class 12 NCERT syllabus.

Frequently asked questions

What is the difference between text and binary files in Python?

Text files store readable characters like letters and digits, while binary files store data in bytes not readable as text, such as images or audio.

Which file mode should I use to add data without deleting existing content?

Use append mode 'a' or 'a+' to add data at the end of a file without overwriting existing content.

How do I check the current position of the file pointer in Python?

Use the method f.tell() to retrieve the current position of the file pointer.

What happens if I open a file in write mode 'w' that already exists?

Opening a file in write mode 'w' overwrites the existing content, erasing previous data.

How can I open a file for both reading and writing in Python?

Use mode 'r+' to open a file for both reading and writing, with the pointer at the start.

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
#binaryfiles#class12#computerscience#filehandling#ncert#programming#python#textfiles

Continue reading