この例を機能させるのを手伝ってもらえますか?
シリアル化された 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()
2 回実行してエラーを確認します。
Traceback (most recent call last):
File "example.py", line 11, in <module>
pickle.dump(cache, cache_file)
IOError: [Errno 9] Bad file descriptor