Skip to content

파일 입출력과 영구 저장 ​

프로그램 변수는 프로그램을 끄면 사라집니다. 그래서 파일에 저장해야 합니다.

python
# 쓰기
with open("data/transactions.jsonl", "a", encoding="utf-8") as f:
    f.write('{"id": "TX-000001", "amount": 15000}\n')

# 읽기
with open("data/transactions.jsonl", encoding="utf-8") as f:
    for line in f:          # 한 줄씩 읽기
        print(line.strip())

포인트:

  • "a" 모드 = 덧붙여 쓰기(기존 내용 유지), "w" 모드 = 통째로 새로 쓰기
  • encoding="utf-8" 필수 (한국어 깨짐 방지)

출처: Codyssey-B1/B2-1