https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

파일 읽기/쓰기 핵심 (한글):

  • open(path, mode, encoding=None) → 파일 객체 반환. mode 기본 ‘r’; ‘w’(덮어쓰기), ‘a’(append), ‘r+‘(읽기/쓰기).

    • 텍스트 모드 vs 바이너리 모드:
    • 기본은 텍스트. 바이너리는 mode에 ‘b’ 추가(‘rb’, ‘wb’, …), encoding 지정 불가.
    • 텍스트 모드는 줄바꿈을 플랫폼별로 변환;
    • 바이너리는 변환 없음(이미지/실행파일은 반드시 바이너리).
  • 인코딩: 명시적으로 encoding=“utf-8” 권장(플랫폼 기본이 다를 수 있음).

    • with 사용 권장:
      with open(“workfile”, “w”, encoding=“utf-8”) as f:
      f.write(“hello”)
  • 직접 열면 f.close()를 호출해야 안전. 닫힌 뒤엔 I/O 불가(ValueError).

  • 쓰기 후 close/with 없이 종료하면 데이터가 일부 안 써질 수 있으니 주의.

← python 3.14으로