HappyWeasel

파이썬 - 파일 읽고 쓰기 본문

Basic/Python

파이썬 - 파일 읽고 쓰기

HappyWeasel 2020. 6. 3. 19:08

파일 읽기

file = open('data.txt')

content = File.read()

file.close()

 

위와 동일하지만 읽고 자동으로 닫기

with open('data.txt') as File:
	content = file.read()

 

 

파일 줄 단위로 읽기

contents = []
with open('data.txt') as file:
	for line in file:
    	content.append(line)

 

파일의 모드

with open('data.txt', 'w') as file:
	file.write('Hello')

w : 쓰기

a : 이어 쓰기

'Basic > Python' 카테고리의 다른 글

파이썬 - 데이터 정렬하기  (0) 2020.06.03
파이썬 - 코드 간결하게  (0) 2020.06.03
파이썬 - 문자열 함수  (0) 2020.06.03
파이썬 - 형 변환  (0) 2020.06.02
파이썬 - 특수연산  (0) 2020.06.02
Comments