Skip to content
C

File Handling

Reading and writing files with open() and the with statement, the four ways to read a file, writing and appending, and working with CSV and JSON files and directories.


So far, every program has "forgotten" everything the moment it stops running — variables disappear once the program ends. File handling lets your programs save data permanently, and read data back from files on disk.


1. What is File Handling?

What is it?

File handling refers to the operations Python provides for creating, reading, writing, and modifying files stored on your computer.

Why do we use it?

  • To save data permanently — so it's still there the next time the program runs.
  • To read existing data — logs, configuration files, datasets.
  • To exchange data between programs, using common formats like CSV and JSON.

Real-World Example

A student record system needs to save student data even after the program closes. A data analysis script needs to read a CSV file of sales data. A logging system needs to continuously write events to a log file.


2. Opening a File — open()

Syntax

python
file = open("filename.txt", "mode")

File Modes

ModeMeaning
"r"Read (default) — file must exist
"w"Write — creates a new file, overwrites if it already exists
"a"Append — adds to the end of an existing file (creates it if missing)
"x"Create — fails if the file already exists
"r+"Read and write
"rb" / "wb"Same as above, but in binary mode (for non-text files like images)

Simple Example

python
file = open("notes.txt", "w") file.write("Hello, this is my first file!") file.close()

Explanation of the Code

  • open("notes.txt", "w") creates the file notes.txt (or overwrites it if it exists).
  • .write() puts text into the file.
  • .close() closes the file, saving the changes and freeing system resources.

Common Mistakes

  • Forgetting to close the file — this can cause data not to be properly saved or the file to remain "locked" by the program.
  • Using "w" mode when you meant "a""w" erases existing content in the file completely.

3. The with Statement (Best Practice)

What is it?

The with statement automatically closes the file for you, even if an error occurs while working with it — this is the recommended, modern way to handle files in Python.

Syntax

python
with open("filename.txt", "mode") as file: # work with file here # file is automatically closed here, even if an error occurred

Simple Example

python
with open("notes.txt", "w") as file: file.write("Hello, this is my first file!") # no need to call file.close() - it happens automatically

Why This Matters

Without with, forgetting .close() (or an error occurring before you reach it) can leave the file open, potentially causing data loss or file-locking issues. with guarantees proper cleanup every time.

Important Points

  • Always prefer with open(...) as file: over manually calling open() and close().
  • This is considered a Python best practice used throughout real-world code.

4. Reading Files

read() — Read the Entire File as One String

python
with open("notes.txt", "r") as file: content = file.read() print(content)

readline() — Read One Line at a Time

python
with open("notes.txt", "r") as file: first_line = file.readline() print(first_line)

readlines() — Read All Lines Into a List

python
with open("notes.txt", "r") as file: lines = file.readlines() print(lines) # ['Line 1\n', 'Line 2\n', 'Line 3\n']

Reading Line by Line with a Loop (Most Common Pattern)

python
with open("notes.txt", "r") as file: for line in file: print(line.strip()) # .strip() removes the trailing newline character

Comparison Table — Reading Methods

MethodReturnsBest For
read()Entire file as one stringSmall files, need full content at once
readline()One line (string)Reading one line at a time manually
readlines()List of all linesNeed each line as a separate list item
for line in file:Iterates line by lineLarge files (memory-efficient), most common pattern

Common Mistakes

  • Trying to open a file that doesn't exist in "r" mode — FileNotFoundError.
  • Forgetting .strip() when printing lines read with readlines() or a for loop, resulting in extra blank lines (since each line already includes its trailing \n).

Practice

  1. Create a text file with 3 lines of your choice, then read and print it using each of the four methods above.

5. Writing to Files

write() — Write a String

python
with open("notes.txt", "w") as file: file.write("First line\n") file.write("Second line\n")

writelines() — Write Multiple Lines at Once

python
lines = ["Line 1\n", "Line 2\n", "Line 3\n"] with open("notes.txt", "w") as file: file.writelines(lines)

Note: writelines() does not automatically add newlines between items — you must include \n yourself in each string, as shown above.

Appending to a File

python
with open("notes.txt", "a") as file: file.write("This line is added at the end\n")

Explanation: "a" mode adds new content after whatever is already in the file, instead of erasing it (unlike "w" mode).

Comparison Table — "w" vs "a"

"w" (Write)"a" (Append)
Existing contentErased completelyPreserved, new content added after
File doesn't existCreates a new fileCreates a new file
Use whenStarting freshAdding to existing data (e.g., logs)

Common Mistakes

  • Using "w" mode repeatedly, accidentally erasing previously saved data every time the program runs.
  • Forgetting \n between lines when using write() multiple times, causing all text to run together on one line.

Practice

  1. Write a program that appends today's date to a file called log.txt every time it runs.

6. Working with CSV Files

What is it?

CSV (Comma-Separated Values) is a simple, widely used format for storing tabular data (like a spreadsheet) as plain text.

Simple Example

python
import csv # Writing a CSV file with open("students.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerow(["Name", "Age", "Course"]) writer.writerow(["Aditi", 21, "Computer Science"]) writer.writerow(["Rohan", 22, "Data Science"]) # Reading a CSV file with open("students.csv", "r") as file: reader = csv.reader(file) for row in reader: print(row)

Output:

['Name', 'Age', 'Course']
['Aditi', '21', 'Computer Science']
['Rohan', '22', 'Data Science']

Reading CSV as Dictionaries (Very Useful)

python
with open("students.csv", "r") as file: reader = csv.DictReader(file) for row in reader: print(row["Name"], row["Course"])

Common Mistakes

  • Forgetting newline="" when opening a CSV file for writing on Windows, which can cause blank extra lines between rows.
  • Forgetting that all values read from a CSV file come back as strings — numbers need manual conversion (int()/float()) if you plan to calculate with them.

Important Points

  • The csv module is Python's built-in tool for reading/writing CSV files.
  • csv.DictReader is often more convenient since it lets you access columns by name instead of position.

7. Working with JSON Files

What is it?

JSON (JavaScript Object Notation) is a text format for structured data, extremely common for configuration files and API communication.

Simple Example

python
import json student = {"name": "Aditi", "age": 21, "course": "Computer Science"} # Writing JSON to a file with open("student.json", "w") as file: json.dump(student, file) # Reading JSON from a file with open("student.json", "r") as file: data = json.load(file) print(data["name"]) # Aditi

Comparison Table — json.dump()/load() vs json.dumps()/loads()

Works WithFunction Names
Files.dump() writes to a file, .load() reads from a filedump / load
Strings.dumps() converts to a string, .loads() converts from a stringdumps / loads (note the extra "s")

Common Mistakes

  • Confusing dump/load (file-based) with dumps/loads (string-based) — the extra "s" makes all the difference.
  • Trying to json.load() a file that isn't valid JSON, causing a json.JSONDecodeError.

Important Points

  • JSON is the standard format for APIs and configuration files — you'll use this constantly in the APIs & HTTP section later.

8. Working with Directories

What is it?

Beyond individual files, Python (via the os module, introduced in the previous file) can create, check, and navigate folders.

Simple Example

python
import os if not os.path.exists("data"): os.mkdir("data") print(os.listdir(".")) # list everything in the current folder print(os.path.isfile("notes.txt")) # True if it's a file print(os.path.isdir("data")) # True if it's a folder

Common Mistakes

  • Trying to create a folder that already exists using os.mkdir(), which raises a FileExistsError — always check with os.path.exists() first.

Common Beginner Mistakes — Summary for This Section

  • Forgetting to close files (always prefer with open(...) as file:).
  • Using "w" mode and accidentally erasing existing data.
  • Forgetting values read from files (including CSV) are always strings.
  • Confusing dump/load with dumps/loads in the json module.
  • Trying to open a non-existent file in read mode.

Cheat Sheet — File Handling

python
with open("file.txt", "r") as f: content = f.read() lines = f.readlines() for line in f: print(line.strip()) with open("file.txt", "w") as f: f.write("text\n") f.writelines(["line1\n", "line2\n"]) with open("file.txt", "a") as f: f.write("appended text\n") import csv csv.writer(file).writerow([...]) csv.reader(file) csv.DictReader(file) import json json.dump(data, file) data = json.load(file) json.dumps(data) # dict -> string json.loads(string) # string -> dict

Mini Project: Student Record Management System

Objective

Build a command-line program that stores student records permanently in a JSON file, so data survives between program runs.

Requirements

  • Add a new student record (name, roll number, marks).
  • View all student records.
  • Save records to a JSON file, and load existing records when the program starts.

Concepts Used

Functions, dictionaries, lists, file handling, JSON, loops, conditionals.

Complete Code

python
import json import os FILENAME = "students.json" def load_records(): if os.path.exists(FILENAME): with open(FILENAME, "r") as file: return json.load(file) return [] def save_records(records): with open(FILENAME, "w") as file: json.dump(records, file, indent=4) def add_student(records): name = input("Name: ") roll_number = input("Roll Number: ") marks = float(input("Marks: ")) records.append({"name": name, "roll_number": roll_number, "marks": marks}) save_records(records) print("Student record added and saved.") def view_students(records): if not records: print("No records found.") return for student in records: print(f"{student['roll_number']} - {student['name']} - Marks: {student['marks']}") records = load_records() while True: print("\n1. Add Student 2. View Students 3. Exit") choice = input("Choose an option: ") if choice == "1": add_student(records) elif choice == "2": view_students(records) elif choice == "3": print("Goodbye!") break else: print("Invalid choice.")

Code Explanation

  • load_records() checks if students.json already exists; if so, it loads previously saved data, so records persist between runs.
  • save_records() writes the current list of records back to the JSON file every time a new student is added — indent=4 makes the saved file human-readable.
  • The while True menu loop keeps the program running until the user exits.

Sample Output

1. Add Student  2. View Students  3. Exit
Choose an option: 1
Name: Aditi
Roll Number: CS101
Marks: 88.5
Student record added and saved.

1. Add Student  2. View Students  3. Exit
Choose an option: 2
CS101 - Aditi - Marks: 88.5

Possible Improvements

  • Add a search function to find a student by roll number.
  • Add an "update" and "delete" option for existing records.
  • Export records to a CSV file for easy viewing in Excel.

Challenge Task

Add a function that calculates and displays the class average and topper from all saved records.


Interview Questions

Q1. Why is the `with` statement preferred over manually calling `open()` and `close()`? Answer: with automatically closes the file when the block finishes, even if an error occurs inside it, preventing resource leaks and data loss.

Q2. What's the difference between `"w"` and `"a"` file modes? Answer: "w" overwrites (erases) existing file content before writing. "a" appends new content to the end of the existing file without erasing it.

Q3. What's the difference between `read()`, `readline()`, and `readlines()`? Answer: read() returns the entire file as one string. readline() returns a single line. readlines() returns a list containing every line as a separate string.

Q4. What's the difference between `json.dump()` and `json.dumps()`? Answer: json.dump() writes JSON data directly to a file. json.dumps() converts data into a JSON-formatted string (no file involved). The equivalent reading functions are json.load() and json.loads().

Q5. What error occurs if you try to open a non-existent file in read mode? Answer: FileNotFoundError.


Practice Questions

Beginner

  1. Create a text file and write 3 lines to it, then read and print its full content.
  2. Append a new line to an existing text file without erasing its content.
  3. Read a file line by line and print only lines that contain the word "Python".
  4. Count the number of lines in a text file.
  5. Check if a file exists before trying to read it.

Intermediate

  1. Write a program that saves a dictionary of student marks to a JSON file, then reads it back and prints it.
  2. Create a CSV file of 5 products with name and price, then read and print it using csv.DictReader.
  3. Write a program that copies the content of one text file into another.
  4. Write a program that counts how many times a specific word appears in a text file.
  5. Write a program that creates a folder if it doesn't already exist, and saves a file inside it.

Challenge

  1. Build a simple note-taking app that lets users add, view, and delete notes stored in a text file.
  2. Write a program that reads a CSV file of sales data and calculates the total and average sale amount.
  3. Extend the Student Record Management mini project to support searching for a student by roll number and updating their marks.

Mock Test

  • File Handling - Quick Test

    10 questions covering open(), the with statement, reading/writing files, and CSV/JSON files.

    10 questions · 10 min · Easy
    Start Mock Test

Coding Problems