파이썬 파일 처리 기본
2019. 12. 31. 01:03ㆍ파이썬 기초
w 모드
file = open("test.txt", "w", encoding="UTF-8")
file.write("텍스트파일에데이터입력")
file.close()
txt파일에 글이 입력된다.
(encoding="UTF-8" 문장은 한글이 깨짐을 방지하려고 넣었습니다.)
a모드
file = open("test.txt", "a", encoding="UTF-8")
file.write("텍스트파일에데이터입력")
file.close()
실행을 시키는 횟수만큼 내용이 텍스트파일에 입력된다.(3번 누르면 3문장 입력)
r모드
file = open("test.txt", "r", encoding="UTF-8")
print(file.read())
file.close()
텍스트파일의 내용을 출력한다.
with 함수
with open("test.txt", "w", encoding="UTF-8") as file:
file.write("안녕하세요.")
with open("test.txt", "r", encoding="UTF-8") as file:
print(file.read())
file open, close를 다 포함하는 문장(코드 줄이기 용이)
'파이썬 기초' 카테고리의 다른 글
파이썬 try except 구문 (0) | 2020.01.02 |
---|---|
파이썬 제너레이터 (0) | 2020.01.02 |
리스트 내포로 map함수 대체하기 (0) | 2019.12.30 |
파이썬 map 함수 (0) | 2019.12.30 |
튜플 정리 (0) | 2019.12.29 |