이 예제를 작성하는 데 도움이 될 수 있습니까?cPickle 사용 중 문제가 발생했습니다.
직렬화 된 dict이있는 경우로드하고 싶습니다. 수정 한 후 다시 덤프하십시오. 파일을 여는 데 사용하는 모드에 문제가 있다고 생각하지만 올바른 방법을 모르겠습니다.
import os
import cPickle as pickle
if os.path.isfile('file.txt'):
cache_file = open('file.txt', 'rwb')
cache = pickle.load(cache_file)
else:
cache_file = open('file.txt', 'wb')
cache = dict.fromkeys([1,2,3])
# modifications of cache
pickle.dump(cache, cache_file)
cache_file.close()
실행이 두 번 오류를 확인합니다 :
Traceback (most recent call last):
File "example.py", line 11, in <module>
pickle.dump(cache, cache_file)
IOError: [Errno 9] Bad file descriptor