📡 You're offline — showing cached content
New version available!
Quick Access
SQL Beginner

Python File I/O: Reading Writing JSON CSV and Binary Files

Complete Python file I/O guide — pathlib, open modes, JSON load/dump, CSV reader/writer, memory-efficient large file processing.

EzyCoders Admin December 18, 2025 10 min read 3 views
Python File IO JSON CSV Binary Guide
Share: Twitter LinkedIn WhatsApp

Python File I/O

File operations are fundamental — reading configs, processing data, writing logs. Python's pathlib and built-in file handling make this clean and Pythonic.

from pathlib import Path

p = Path('/var/log/app.log')
print(p.name)       # 'app.log'
print(p.suffix)     # '.log'
print(p.exists())   # True/False

content = p.read_text(encoding='utf-8')
lines   = p.read_text().splitlines()

Path('output.txt').write_text('Hello World\n', encoding='utf-8')

JSON and CSV

import json, csv

# JSON
data = {'users': [{'id': 1, 'name': 'Rahul'}]}
Path('data.json').write_text(json.dumps(data, indent=2))
users = json.loads(Path('data.json').read_text())

# CSV reading
with open('users.csv', 'r', newline='', encoding='utf-8') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row['name'], row['email'])

# CSV writing
with open('report.csv', 'w', newline='', encoding='utf-8') as f:
    writer = csv.DictWriter(f, fieldnames=['name', 'email', 'score'])
    writer.writeheader()
    writer.writerows([{'name': 'Rahul', 'email': 'r@e.com', 'score': 95}])

Memory-Efficient Large Files

def process_large_csv(filepath: str, chunk_size: int = 10_000):
    buffer = []
    with open(filepath, 'r') as f:
        reader = csv.DictReader(f)
        for row in reader:
            buffer.append(row)
            if len(buffer) >= chunk_size:
                process_chunk(buffer)
                buffer.clear()
    if buffer:
        process_chunk(buffer)

Q: What does newline='' mean in CSV open()?

The csv module handles its own line endings. Opening with newline='' tells Python NOT to translate \r\n to \n before the csv module sees it — otherwise extra blank rows appear on Windows.

EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment