import pickle, MyScore, zipfile
if __name__ == '__main__':
f = open('pickle_01.txt', 'wb')
s = [MyScore.Score('홍길동', 89, 75, 100),
MyScore.Score('홍길동', 56, 100, 67),
MyScore.Score('홍길동', 100, 78, 95)]
pickle.dump(s,f)
f = open('pickle_01.txt', 'rb')
res = pickle.load(f)
for r in range(len(res)) :
print(res[r])
#pickle_01.txt 파일을 zip파일로 인서트하자.
with zipfile.ZipFile('B:/pythonPickle/com/test01/my_shutil.zip','a') as zf:
zf.write('B:/pythonPickle/com/test01/pickle_01.txt')
# zip파일 안에 a.txt 파일에 내용을 써보자.
with zipfile.ZipFile('B:/pythonPickle/com/test01/my_shutil.zip','a') as zf:
with zf.open('/a.txt','w') as f:
f.write(b'abcdeg');
with zipfile.ZipFile('B:/pythonPickle/com/test01/my_shutil.zip', 'a') as zf:
print(zf.namelist())
출력결과
홍길동 89 75 100 26488.0 학점 : B
홍길동 56 100 67 22374.33333333333333 학점 : C
홍길동 100 78 95 27391.0 학점 : A
['pythonPickle/com/test01/pickle_01.txt', '/a.txt']
실행전 | 실행후 |
---|---|
![]() | ![]() |
my_shutil.zip파일과 pickle_01.txt 파일 생성됨 |
pickle.dump(s,f)
로 list 객체를 파일에 저장했다.for
문과 print
를 활용하여 list객체를 순서대로 출력해보았다.with ~ as
문을 활용하여 자동 파일 닫기를 구현하며 pickle.txt 파일을 zip파일로 인서트했다.GUI상 txt파일 살펴보기 |
---|
![]() |
f = open('pickle_01.txt', 'rb')
와 같이 rb
모드로 설정해주어야 한다.print(zf.namelist())
를 통해 zip파일 안의 모든 파일 리스틀 확인했다.