[2026] Python File Handling | Read, Write, CSV, JSON, and pathlib

[2026] Python File Handling | Read, Write, CSV, JSON, and pathlib

이 글의 핵심

Learn Python file I/O: text files, CSV with csv module, JSON with json, pathlib paths, encoding, and the with statement—practical patterns included.

Introduction

“Files are everyday work”

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

1. Text files

Reading

다음은 python를 활용한 상세한 구현 코드입니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

# 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

아래 코드는 python를 사용한 구현 예제입니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

# 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

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

아래 코드는 python를 사용한 구현 예제입니다. 필요한 모듈을 import하고. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고, 함수를 통해 로직을 구현합니다, 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고, 클래스를 정의하여 데이터와 기능을 캡슐화하며. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고, 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

# ✅ 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


... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3