Python File Handling | Read, Write, CSV, JSON, and pathlib

Python File Handling | Read, Write, CSV, JSON, and pathlib

이 글의 핵심

Practical guide to Python file handling: reading and writing text, CSV and JSON, pathlib, and safe patterns with encoding and context managers.

Introduction

“Files are everyday work”

Reading and writing files underpins data persistence, logging, and configuration in real projects.


1. Text files

Reading

# Read entire file
with open('data.txt', 'r', encoding='utf-8') as f:
    content = f.read()
    print(content)

# Read all lines into a list
with open('data.txt', 'r', encoding='utf-8') as f:
    lines = f.readlines()
    for line in lines:
        print(line.strip())

# Iterate line by line (memory-friendly)
with open('data.txt', 'r', encoding='utf-8') as f:
    for line in f:
        print(line.strip())

Writing

# Overwrite (w)
with open('output.txt', 'w', encoding='utf-8') as f:
    f.write("First line\n")
    f.write("Second line\n")

# Append (a)
with open('output.txt', 'a', encoding='utf-8') as f:
    f.write("Third line\n")

# Write multiple lines
lines = ["line 1\n", "line 2\n", "line 3\n"]
with open('output.txt', 'w', encoding='utf-8') as f:
    f.writelines(lines)

2. CSV files

Reading CSV

import csv

# As rows (lists)
with open('data.csv', 'r', encoding='utf-8') as f:
    reader = csv.reader(f)
    header = next(reader)  # first row (header)
    
    for row in reader:
        print(row)

# As dict rows (recommended)
with open('data.csv', 'r', encoding='utf-8') as f:
    reader = csv.DictReader(f)
    
    for row in reader:
        print(row['name'], row['age'])

Writing CSV

import csv

# Write rows as lists
data = [
    ['Name', 'Age', 'City'],
    ['Alice', 25, 'Seoul'],
    ['Bob', 30, 'Busan']
]

with open('output.csv', 'w', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    writer.writerows(data)

# Write dict rows
data = [
    {'name': 'Alice', 'age': 25, 'city': 'Seoul'},
    {'name': 'Bob', 'age': 30, 'city': 'Busan'}
]

with open('output.csv', 'w', newline='', encoding='utf-8') as f:
    fieldnames = ['name', 'age', 'city']
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    
    writer.writeheader()
    writer.writerows(data)

3. JSON files

Reading JSON

import json

# From file
with open('data.json', 'r', encoding='utf-8') as f:
    data = json.load(f)
    print(data)

# From string
json_str = '{"name": "Alice", "age": 25}'
data = json.loads(json_str)
print(data['name'])  # Alice

Writing JSON

import json

data = {
    "name": "Alice",
    "age": 25,
    "hobbies": ["reading", "movies", "sports"],
    "address": {
        "city": "Seoul",
        "district": "Gangnam"
    }
}

# Write to file
with open('output.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

# Serialize to string
json_str = json.dumps(data, ensure_ascii=False, indent=2)
print(json_str)

4. Paths with pathlib

Using pathlib

from pathlib import Path

# Current directory
current = Path.cwd()
print(current)

# Join paths
data_dir = Path('data')
file_path = data_dir / 'users.json'
print(file_path)  # data/users.json

# Exists?
if file_path.exists():
    print("file exists")

# Create directory
data_dir.mkdir(exist_ok=True)

# Read/write text
file_path.write_text("Hello, World!", encoding='utf-8')
content = file_path.read_text(encoding='utf-8')
print(content)

# Path parts
print(file_path.name)    # users.json
print(file_path.stem)    # users
print(file_path.suffix)  # .json
print(file_path.parent)  # data

5. Practical examples

Log analysis

from collections import Counter
from pathlib import Path

def analyze_log(log_file):
    """Count error types from a log file."""
    error_counts = Counter()
    
    with open(log_file, 'r', encoding='utf-8') as f:
        for line in f:
            if 'ERROR' in line:
                error_type = line.split(':')[1].strip()
                error_counts[error_type] += 1
    
    return error_counts

# Usage
errors = analyze_log('app.log')
for error, count in errors.most_common(5):
    print(f"{error}: {count} occurrences")

Simple config helper

import json
from pathlib import Path

class Config:
    def __init__(self, config_file='config.json'):
        self.config_file = Path(config_file)
        self.data = self.load()
    
    def load(self):
        if self.config_file.exists():
            with open(self.config_file, 'r', encoding='utf-8') as f:
                return json.load(f)
        return {}
    
    def save(self):
        with open(self.config_file, 'w', encoding='utf-8') as f:
            json.dump(self.data, f, ensure_ascii=False, indent=2)
    
    def get(self, key, default=None):
        return self.data.get(key, default)
    
    def set(self, key, value):
        self.data[key] = value
        self.save()

# Usage
config = Config()
config.set('database_url', 'localhost:5432')
print(config.get('database_url'))

Practical tips

File handling tips

# ✅ Use with (automatic close)
with open('file.txt', 'r') as f:
    content = f.read()

# ❌ Manual close (can leak on exceptions)
f = open('file.txt', 'r')
content = f.read()
f.close()

# ✅ Always specify encoding for text
with open('file.txt', 'r', encoding='utf-8') as f:
    pass

# ✅ Check existence first
from pathlib import Path
if Path('file.txt').exists():
    pass

Summary

Key takeaways

  1. Text files: with open(path, mode, encoding='utf-8') for safe I/O.
  2. CSV: use csv.reader / csv.writer or DictReader / DictWriter; set newline='' on write.
  3. JSON: json.load / json.dump for files; mind ensure_ascii and indent for readability.
  4. Paths: pathlib.Path for joining paths and reading small text files.
  5. Context managers: prefer with so files close even when errors occur.

Next steps