Python File I/O: Reading and Writing JSON, CSV, and Text Files Like a Pro

Welcome to Cyber Supto! I'm Supto.

Handling files is a core skill for any Python developer. From storing data to reading configurations, file operations allow your programs to interact with the outside world. In this guide, we will explore Python File I/O in depth, covering text files, CSV files, and JSON files, along with professional tips for robust data handling.

Table of Contents

  • Understanding File I/O in Python
  • Working with Text Files
  • Working with CSV Files
  • Working with JSON Files
  • Best Practices for File Handling
  • Common Mistakes
  • Frequently Asked Questions

Understanding File I/O in Python

Python provides built-in functions to handle files. The basic workflow is:

  1. Open the file
  2. Read or write data
  3. Close the file

Example of reading a text file:

file = open("example.txt", "r")
content = file.read()
print(content)
file.close()

Output (depends on file content):

Hello Cyber Supto!
Welcome to Python File I/O tutorial.

Using with statement is preferred as it automatically closes files:

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

Working with Text Files

Writing to Text Files

Open the file in write mode ("w") to create or overwrite a file:

with open("output.txt", "w") as file:
    file.write("This is Cyber Supto tutorial.\n")
    file.write("Python File I/O is powerful!")

Appending to Files

Use append mode ("a") to add content without overwriting:

with open("output.txt", "a") as file:
    file.write("\nAdding a new line at the end.")

Reading Files Line by Line

with open("output.txt", "r") as file:
    for line in file:
        print(line.strip())

strip() removes extra newline characters for cleaner output.

Working with CSV Files

CSV (Comma Separated Values) is widely used for tabular data. Python provides the csv module.

Reading CSV Files

import csv

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Output (example):

['Name', 'Age', 'Country']
['Supto', '21', 'Bangladesh']
['Alice', '25', 'USA']

Writing CSV Files

import csv

data = [
    ["Name", "Age", "Country"],
    ["Supto", 21, "Bangladesh"],
    ["Alice", 25, "USA"]
]

with open("output.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerows(data)

Using DictReader and DictWriter

DictReader and DictWriter provide a more intuitive interface using dictionaries:

import csv

# Reading
with open("data.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row['Name'], row['Age'])

# Writing
with open("output_dict.csv", "w", newline="") as file:
    fieldnames = ["Name", "Age", "Country"]
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({"Name":"Bob", "Age":30, "Country":"Canada"})

Working with JSON Files

JSON (JavaScript Object Notation) is ideal for storing structured data. Python provides the json module.

Reading JSON

import json

with open("data.json", "r") as file:
    data = json.load(file)
    print(data)

Example data.json:

{
    "name": "Supto",
    "age": 21,
    "skills": ["Python", "Blogger", "Web Development"]
}

Output:

{'name': 'Supto', 'age': 21, 'skills': ['Python', 'Blogger', 'Web Development']}

Writing JSON

import json

data = {
    "name": "Alice",
    "age": 25,
    "skills": ["Python", "Data Science"]
}

with open("output.json", "w") as file:
    json.dump(data, file, indent=4)

indent=4 makes JSON human-readable.

Appending Data to JSON

Unlike text files, JSON files need to be read, modified, and written back:

import json

with open("output.json", "r") as file:
    data = json.load(file)

data["country"] = "USA"

with open("output.json", "w") as file:
    json.dump(data, file, indent=4)

Best Practices for File Handling

  • Always use with statement to automatically close files.
  • Use try-except blocks to handle file errors.
  • Use csv.DictReader and DictWriter for clarity.
  • Use json.load and json.dump for structured data.
  • Keep file paths relative to your project for portability.
  • Handle encoding explicitly using encoding='utf-8'.

Common Mistakes

Mistake Explanation
Not closing files Leads to resource leaks or locked files
Overwriting files unintentionally Always verify mode ('w' vs 'a')
Ignoring exceptions Use try-except to prevent program crashes
Reading JSON as text Use json.load() for proper parsing
Using hard-coded paths Prefer relative paths or config files

Real-World Example: Combining CSV and JSON

Suppose you want to read a CSV file and save it as JSON:

import csv
import json

csv_data = []
with open("data.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        csv_data.append(row)

with open("data.json", "w") as file:
    json.dump(csv_data, file, indent=4)

This converts tabular data to structured JSON for APIs or web apps.

Frequently Asked Questions

Question Answer
What is the difference between "r", "w", and "a" modes? "r" = read, "w" = write (overwrite), "a" = append.
How do I handle missing files? Use try-except for FileNotFoundError.
Can I read large files line by line? Yes, using a loop: for line in file:
How to format JSON for readability? Use indent parameter in json.dump()
Can I mix CSV and JSON? Yes, read CSV, convert to dict/list, then save as JSON.

Conclusion

Mastering file I/O is essential for professional Python development. With these skills, you can:

  • Read and write text files efficiently
  • Work with structured data in CSV and JSON formats
  • Handle errors gracefully using try-except
  • Build robust and scalable applications

Thanks for reading on Cyber Supto! I'm Supto. Keep exploring Python File I/O and build professional applications that can read, write, and manage data like a pro!