개발로그필름
[python] 파일 입출력 본문
728x90
반응형
SMALL
# 두가지 방법으로 파일 읽어오기
score_file = open("score.txt", "w", encoding="utf8")
print("수학 : 0", file=score_file)
print("영어 : 50", file=score_file)
score_file.close()
score_file = open("score.txt", "a", encoding="utf8")
score_file.write("과학 : 80")
score_file.write("\n코딩 : 100")
score_file.close()
# 파일 써보기
score_file = open("score.txt", "r", encoding="utf8")
print(score_file.read())
score_file.close()
# 한줄 한줄 불러와서 출력하기
score_file = open("score.txt", "r", encoding="utf8")
print(score_file.readline(), end="") # 줄 별로 읽기, 한 줄 읽고 커서는 다음 줄로 이동
print(score_file.readline(), end="")
print(score_file.readline(), end="")
print(score_file.readline(), end="")
score_file.close()
# 몇줄인지 모를 때
score_file = open("score.txt", "r", encoding="utf8")
while True:
line = score_file.readline()
if not line:
break
print(line, end="")
score_file.close()
# list에 저장해서 출력
score_file = open("score.txt", "r", encoding="utf8")
lines = score_file.readlines() # list 형태로 저장
for line in lines:
print(line, end="")
score_file.close()
반응형
LIST
'IT > Python' 카테고리의 다른 글
[python] 가변인자 / 지역변수와 전역변수 (0) | 2022.11.21 |
---|---|
[python] 표준 입출력 / 다양한 출력 포맷 (2) | 2022.11.20 |
[python] 예외처리, 에러 발생시키기, 사용자 정의 예외처리, finally (0) | 2022.10.13 |
[python] 파이썬 퀴즈 1 (0) | 2022.10.13 |
[Python] 모듈 & 패키지 (0) | 2022.10.01 |
Comments