728x90
반응형
# pickle모듈
# 1) 파일에 파이썬 객체 저장하기
import pickle
data = {
"목표1" : "돈모으기",
"목표2" : "취업하기"
}
file = open(data.pickle, "wb") # wb : 바이너리모드(컴퓨터가 바로 읽을 수 있는 모드)
pickle.dump(data, file) # dump함수 : data객체와 file 넘겨주기
file.close()
# pickle모듈
# 2) 파일로부터 파이썬 객체 읽기
import pickle
# with구문 사용X
file = open("data.pickle", "rb")
data = pickle.load(file) # 파일 읽어주는 함수
#print(data)
file.close()
# with구문 사용 -> file.close() 자동 호출
with open("data.txt", "r") as file:
data = file.read()
728x90
반응형
'Programming > Python' 카테고리의 다른 글
[Python] 파이썬 예외처리 (0) | 2025.02.02 |
---|---|
[Python] 파이썬 파일입출력 (0) | 2025.02.02 |
[Python] 파이썬 모듈/패키지 (0) | 2025.02.02 |
[Python] 파이썬 상속 오버라이딩 / 클래스 변수 (0) | 2025.01.31 |
[Python] 파이썬 상속 - 오버라이딩/오버로딩 (0) | 2025.01.31 |