본문으로 건너뛰기
Previous
Next
Python File Handling | Read, Write, CSV, JSON, and pathlib

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

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

# 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

  • [Exception handling](/en/blog/python-series-08-exception-handling/
  • File automation (later in the series)

  • Browse the Python series
  • [Python environment setup | Install Python on Windows and Mac](/en/blog/python-series-01-environment-setup/
  • [Python basics | Variables, operators, control flow](/en/blog/python-series-02-basic-syntax/
  • [Python data types | Lists, dicts, tuples, sets](/en/blog/python-series-03-data-types/

자주 묻는 질문 (FAQ)

Q. 이 내용을 실무에서 언제 쓰나요?

A. Learn Python file I/O: text files, CSV with csv module, JSON with json, pathlib paths, encoding, and the with statement—… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.

Q. 선행으로 읽으면 좋은 글은?

A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. Python 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.

Q. 더 깊이 공부하려면?

A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.


같이 보면 좋은 글 (내부 링크)

이 주제와 연결되는 다른 글입니다.

  • [Python Exception Handling | try-except· else](/en/blog/python-series-08-exception-handling/
  • [Node.js File System: Complete Guide to the fs Module](/en/blog/nodejs-series-05-filesystem/
  • [C++ std::filesystem Complete Guide | Cross-Platform Path](/en/blog/cpp-series-37-1-filesystem-basics/

이 글에서 다루는 키워드 (관련 검색어)

Python, File I/O, CSV, JSON, pathlib, with Statement 등으로 검색하시면 이 글이 도움이 됩니다.