Python

[Python] pickle 파일 작성 및 읽기

MinEtoile 2024. 1. 29. 21:08

Pickle 파일 작성

import pickle
data = {
	"계획1" : "논문 스터디",
    	"계획2" : "중국어 공부"
}
file = open("data.pickle", "wb")  # wb: wrtie binary -> binary 형태로, 컴퓨터가 바로 읽을 수 있음
pickle.dump(data, file)  # data를 file에 담음
file.close()

 

Pickle 파일 읽기

import pickle
file = open("data.pickle", "rb")
data = pickle.load(file)
print(data)
file.close()

 

With 구문

# with 구문 X
file = open("data.txt", "r", encoding = "utf8")
data = file.read()
file.close()

# with 구문 O
with open("file.txt", "r", encoding = "utf8") as file:
	data = file.read()