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 : 이어 쓰기