#================ 파일 입출력 =================================
score_file = open("score.txt","w", encoding="utf8") # 쓰기
print("수학 : 0", file=score_file)
print("영어 : 0", file=score_file)
score_file.close()
score_file = open("score.txt","a", encoding="utf8") # 수정- 줄바꿈 안되서 \n 추가
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()
#for문으로 한줄씩 읽기
print("====================for문으로 한줄씩 읽기===========================\n")
score_file = open("score.txt","r", encoding="utf8") # 읽기
while True:
line = score_file.readline()
if not line:
break
print(line, end="")
score_file.close()
print("====================for문으로 한줄씩 읽기===========================\n")
#list 에 담기
print("=======================list 에 담기==================================\n")
score_file = open("score.txt","r", encoding="utf8") # 읽기
lines = score_file.readlines() # readlines
for line in lines:
print(line , end="")
score_file.close()
print("=======================list 에 담기===================================\n")
print("=========================================================================")
#################################================ pickle - 데이터를 파일 형태로 처리=================================################################
import pickle
profile_file = open("profile.pickle","wb") #wb - w : 쓰기 b = 바이너리
profile = {"이름" : "혼길동","나이":30 , "취미" : ["zz","rr","놀이"]}
print(profile)
pickle.dump(profile,profile_file ) # profile에 있는 정보를 profile_file에 저장
profile_file.close()
#####################################################################################
profile_file_r = open("profile.pickle","rb") #rb - r : 읽기 b = 바이너리
profile_r = pickle.load(profile_file_r)
print(profile_r)
profile_file_r.close()
#####################################################################################
with open("profile.pickle","rb") as profile_file:
print(pickle.load(profile_file))
# close 필요없음
'''
with open("study.txt" , "w" , encoding="utf8") as study_file:
study_file.write("파이썬을 공부하자")
'''
with open("study.txt" , "r" , encoding="utf8") as study_file:
print(study_file.read())
#########################################################################################################################
##################################################### quiz ##############################################################
'''
1주차 ~ 50주차까지 보고서 생성 파일
주건 : 파일명은 'X주차.txt'
'''
def create_file(num):
score_file = open("C:\pythonWorkspace\file\"+str(num)+"주차.txt","w", encoding="utf8") # 쓰기
print("- {}주차 주간보고 - ".format(num), file=score_file)
print("부서 : ", file=score_file)
print("이름 : ", file=score_file)
print("업무 요약 : ", file=score_file)
score_file.close()
for i in range(1,51):
create_file(i)
##또는
for i in range(1,51):
with open("C:\pythonWorkspace\file\"+str(i)+"주차주간보고.txt" , "w" , encoding="utf8") as study_file:
study_file.write("- "+ str(i) +"주차 주간보고 - ")
study_file.write("\n부서 : ")
study_file.write("\n이름 : ")
study_file.write("\n업무 요약 : ")
#########################################################################################################################