开发者

Problem using cPickle

Could y开发者_如何学编程ou helpme to make this exmaple work?

I'd like to load a serialized dict if it exists, modify it and dump it again. I think I have a problem with the mode I'm using to open the file but I don't know the correct way.

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()    

Run it twice to see the error:

Traceback (most recent call last):
  File "example.py", line 11, in <module>
    pickle.dump(cache, cache_file)
IOError: [Errno 9] Bad file descriptor


'rwb' is not correct file open mode for open(). Try 'r+b'.

And after you have read from file, you have cursor positioned at the end of file, so pickle.dump(cache, cache_file) will append to the file (which is probably not what you want). Try cache_file.seek(0) after pickle.load(cache_file).


For each load, you need to open(with mode='rb'), load, and close the file handle.
For each dump, you need to open(with mode='wb'), dump, and close the file handle.


You have opened the file for reading and writing - i.e. random access. When you initially read the file you leave the file index position at the end of the file, so when you later write the data back you are appending to the same file.

You should open the file in read mode, read the data, close it, then reopen in write mode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜