Reading and Writing Files
Why File Handling?
So far everything in your programs is temporary. When the program closes — all data is gone.
Contact book banaya? Program band kiya — sab contacts gone. Shopping cart banaya? Program band kiya — sab items gone.
File handling lets you permanently save data to a file on your computer so it persists even after the program closes.
How File Handling Works
Three basic operations:
- Write — save data to a file
- Read — load data from a file
- Append — add more data to existing file
Opening a File — open()
file = open("filename.txt", "mode")
Modes:
| Mode | Meaning |
|---|---|
"r" |
Read — open existing file to read |
"w" |
Write — create new file or overwrite existing |
"a" |
Append — add to end of existing file |
"x" |
Create — create new file, error if already exists |
Writing to a File
file = open("notes.txt", "w") file.write("Hello, this is my first file!\n") file.write("I am learning Python file handling.\n") file.write("This is line 3.\n") file.close()
This creates a file called notes.txt in the same folder as your Python file and writes those three lines into it.
Important — always call file.close() when done. If you forget, data might not save properly.
The Better Way — with Statement
Instead of manually closing, use with — it automatically closes the file when done:
with open("notes.txt", "w") as file: file.write("Hello, this is my first file!\n") file.write("I am learning Python file handling.\n") file.write("This is line 3.\n")
# file is automatically closed here print("File written successfully!")
Always use with — it's cleaner, safer, and the standard way in Python.
Reading a File
Read entire file at once
with open("notes.txt", "r") as file: content = file.read() print(content)
Output:
Hello, this is my first file!
I am learning Python file handling.
This is line 3.
Read line by line — readline()
with open("notes.txt", "r") as file: line1 = file.readline() # reads first line line2 = file.readline() # reads second line print(line1) print(line2)
Read all lines into a list — readlines()
with open("notes.txt", "r") as file: lines = file.readlines() # returns a list of all lines print(lines)
Output:
['Hello, this is my first file!\n', 'I am learning Python file handling.\n', 'This is line 3.\n']
Notice each line has \n at the end. Use .strip() to remove it:
with open("notes.txt", "r") as file: lines = file.readlines()
for line in lines: print(line.strip()) # removes \n from each line
Best way to read — Loop directly
with open("notes.txt", "r") as file: for line in file: print(line.strip())
This is memory efficient — reads one line at a time instead of loading the whole file at once. Best for large files.
Appending to a File
"w" mode overwrites everything. Use "a" to add to existing content:
# First write with open("notes.txt", "w") as file: file.write("Line 1\n") file.write("Line 2\n")
# Append more — existing content stays with open("notes.txt", "a") as file: file.write("Line 3\n") file.write("Line 4\n")
# Read and verify with open("notes.txt", "r") as file: print(file.read())
Output:
Line 1
Line 2
Line 3
Line 4
File Does Not Exist — Handling Errors
If you try to read a file that doesn't exist:
with open("missing.txt", "r") as file: # FileNotFoundError! content = file.read()
Handle it properly:
try: with open("missing.txt", "r") as file: content = file.read() print(content) except FileNotFoundError: print("File not found! Please check the filename.")
We'll cover error handling in full detail in Stage 7. For now just know this pattern exists.
Checking if File Exists
import os
if os.path.exists("notes.txt"): print("File exists") else: print("File does not exist")
import os gives you access to operating system features like checking files, creating folders, etc. We'll cover imports properly in Stage 8.
Writing Multiple Lines at Once — writelines()
lines = [ "First line\n", "Second line\n", "Third line\n" ]
with open("notes.txt", "w") as file: file.writelines(lines)
Note — writelines() does NOT automatically add \n. You have to include it in each string yourself.
Real World Example 1 — Save and Load a To-Do List
FILENAME = "todos.txt"
def load_todos(): todos = [] try: with open(FILENAME, "r") as file: for line in file: todos.append(line.strip()) except FileNotFoundError: pass # file doesn't exist yet — that's fine, start with empty list return todos
def save_todos(todos): with open(FILENAME, "w") as file: for todo in todos: file.write(todo + "\n")
def show_todos(todos): if len(todos) == 0: print("No tasks yet!") else: print("\n=== Your To-Do List ===") for i, todo in enumerate(todos, 1): print(f"{i}. {todo}")
def add_todo(todos): task = input("Enter task: ") todos.append(task) save_todos(todos) print("Task added and saved!")
def delete_todo(todos): show_todos(todos) if len(todos) > 0: num = int(input("Enter task number to delete: ")) if 1 <= num <= len(todos): removed = todos.pop(num - 1) save_todos(todos) print(f"Deleted: {removed}") else: print("Invalid number")
# Load existing todos when program starts todos = load_todos()
while True: print("\n=== To-Do App ===") print("1. View tasks") print("2. Add task") print("3. Delete task") print("4. Exit")
choice = input("Enter choice: ")
if choice == "1": show_todos(todos) elif choice == "2": add_todo(todos) elif choice == "3": delete_todo(todos) elif choice == "4": print("Goodbye!") break else: print("Invalid choice")
Now your to-do list persists — close the program, open again — your tasks are still there. That's the power of file handling.
Real World Example 2 — Simple Logger
Logging is recording what happens in your program over time:
from datetime import datetime
def log(message): timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") log_entry = f"[{timestamp}] {message}\n"
with open("app_log.txt", "a") as file: file.write(log_entry)
print(log_entry.strip())
log("Program started") log("User logged in: Gagan") log("User searched for: Python tutorials") log("User logged out") log("Program ended")
Output in terminal and saved to app_log.txt:
[2025-03-03 10:30:01] Program started
[2025-03-03 10:30:01] User logged in: Gagan
[2025-03-03 10:30:01] User searched for: Python tutorials
[2025-03-03 10:30:01] User logged out
[2025-03-03 10:30:01] Program ended
datetime is a built-in Python module — we'll cover modules in Stage 8. For now just know this is how you get current date and time.
Working with CSV Files
CSV (Comma Separated Values) is the most common file format for data. Excel files, database exports — almost everything can be saved as CSV.
A CSV file looks like this:
name,age,city,marks
Rahul,20,Delhi,85
Priya,21,Mumbai,92
Gagan,22,Delhi,78
Writing a CSV
# Writing CSV manually students = [ ["name", "age", "city", "marks"], # header row ["Rahul", 20, "Delhi", 85], ["Priya", 21, "Mumbai", 92], ["Gagan", 22, "Delhi", 78] ]
with open("students.csv", "w") as file: for row in students: line = ",".join(str(item) for item in row) file.write(line + "\n")
print("CSV file created!")
Reading a CSV
with open("students.csv", "r") as file: lines = file.readlines()
# First line is header header = lines[0].strip().split(",") print(f"Columns: {header}")
# Rest are data rows for line in lines[1:]: data = line.strip().split(",") print(f"Name: {data[0]}, Age: {data[1]}, City: {data[2]}, Marks: {data[3]}")
Output:
Columns: ['name', 'age', 'city', 'marks']
Name: Rahul, Age: 20, City: Delhi, Marks: 85
Name: Priya, Age: 21, City: Mumbai, Marks: 92
Name: Gagan, Age: 22, City: Delhi, Marks: 78
Python also has a built-in csv module that handles all edge cases — we'll see that in Stage 8.
File Paths
By default Python looks for files in the same folder as your Python script. But you can specify full paths:
# Windows with open("C:\\Users\\Gagan\\Desktop\\notes.txt", "w") as file: file.write("Hello")
# Mac/Linux with open("/home/gagan/Desktop/notes.txt", "w") as file: file.write("Hello")
# Better way — relative path (subfolder) with open("data/notes.txt", "w") as file: # saves in 'data' subfolder file.write("Hello")
For now just keep all your files in the same folder as your Python script — much simpler.
Quick Reference
# Write (creates or overwrites) with open("file.txt", "w") as f: f.write("text\n")
# Append (adds to existing) with open("file.txt", "a") as f: f.write("more text\n")
# Read entire file with open("file.txt", "r") as f: content = f.read()
# Read line by line with open("file.txt", "r") as f: for line in f: print(line.strip())
# Read all lines into list with open("file.txt", "r") as f: lines = f.readlines()
Exercise 🏋️
Build a Personal Diary app with file persistence:
- When program starts — load existing diary entries from
diary.txt - Show a menu:
=== My Diary ===
1. Write new entry
2. Read all entries
3. Read today's entries
4. Exit
- Write entry — automatically add today's date and time before the entry, save to file
- Read all entries — display everything from the file
- Read today's entries — show only entries from today's date
Expected file format:
[2025-03-03 09:15:00] Today was a great day. I learned file handling in Python.
[2025-03-03 14:30:00] Just had lunch and back to coding!
[2025-03-04 10:00:00] New day, new learning!
Hint for today's date: datetime.now().strftime("%Y-%m-%d")
Hint for checking today's entries: check if each line startswith today's date string wrapped in [
No comments:
Post a Comment