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 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.csvfiles. 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 Mode | Description | File Pointer Position |
|---|---|---|
r | Read-only mode. File must exist. | Start of file |
rb | Read-only binary mode. | Start of file |
r+ or +r | Read and write mode. File must exist. | Start of file |
w | Write mode. Overwrites existing file or creates new. | Start of file |
wb+ or +wb | Read, write, binary mode. Overwrites or creates new. | Start of file |
a | Append mode. Adds data at end or creates new file. | End of file |
a+ or +a | Append 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(), orreadlines()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
ato 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
randw. - 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:
| Mode | Purpose | Overwrites File? | File Pointer Start |
|---|---|---|---|
r | Read-only | No | Beginning |
w | Write-only | Yes | Beginning |
a | Append-only | No | End |
r+ | Read and write | No | Beginning |
a+ | Append and read | No | End |
rb | Read binary | No | Beginning |
wb+ | Read, write binary | Yes | Beginning |
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.
Study smarter with ConceptScroll
Daily NCERT-aligned reels, AI doubt solving and chapter quizzes — all free.
Start learning freeContinue reading
- Stack Data Structure: Complete Guide for Class 12 Computer Science
Explore the stack data structure, a fundamental linear data structure in Class 12 NCERT Computer Science. Understand its operations, implementation, and real-world uses.
- Understanding Stack in Class 12 Computer Science: Concepts & Applications
Explore the Stack data structure as per Class 12 NCERT Computer Science syllabus. Understand its definition, operations, and real-life applications with examples.
- Stack in Computer Science: Class 12 NCERT Guide with Python Implementation
Explore the Stack data structure from Class 12 NCERT Computer Science. Understand its operations, implementation in Python, and key concepts for exams.