파이썬 파일입출력

1 개념[ | ]

python files
  • 파이썬으로 다른 파일 내용을 읽고 쓰기가 가능

2 파일 열기[ | ]

  • open("파일이름", "모드")

열기 모드

모드 설명
r 읽기 모드
w 쓰기 모드
a 추가 모드
b 바이너리 모드

3 읽기 예시[ | ]

open.py 파일이 hello.txt 파일을 열어 그 내용을 출력
  • open.py
file = open("hello.txt", "r")
content = file.read()
print(content)
  • hello.txt
Hello World!

4 쓰기 예시[ | ]

file = open('hello.txt', "w")
file.write("Hello John")
file.close()

file = open("./hello.txt", "r")
content = file.read()
print(content)
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}