score_file = open('score.txt','w',encoding='utf8')
print('수학:0',file=score_file)
print('영어:19',file=score_file)
score_file.close() > 마지막에 무조건 닫아주기!!
=====w는 쓰기 용도, a는 그 파일에 이어서 쓰기=====
score_file = open('score.txt','a',encoding='utf8')
score_file.write('과학:100')
score_file.write('\n코딩:100')
score_file.close()
score_file = open('score.txt','r',encoding='utf8')
print(score_file.read())
#한 줄씩 불러오기
score_file = open('score.txt','r',encoding='utf8')
print(score_file.readline())
#몇 줄인지 모를 땐, 반복문 통해서 파일 불러오기
score_file = open('score.txt','r',encoding='utf8')
while True:
line = score_file.readline()
if not line:
break
print(line)
score_file.close()
읽어오기, 마지막 닫기 하나하나 쓸 필요 없는 편리한 함수!!
with open('study.txt','w', encoding='uft8') as study_file: study_file.write('파이썬을 공부하고 있어요')