加载pickle数据时cPickle EOFError

时间:2014-09-14 19:17:46

标签: python macos python-2.7 pickle

编辑:我很蠢

对不起大家。我的循环不起作用的原因是一个非常愚蠢的原因;我对pickle.dump()的参数输入错误。

OP

第一次腌制物品,对不起,如果这是一个小问题;已经查看了其他各种EOFError帖子,但没有一个工作或似乎适用。

此外,虽然我关闭了.pkl文件,但当脚本运行时,它始终具有零字节文件大小。我不明白为什么。

我在OS X(小牛队);这是我的代码(为了便于阅读而略微减少; to_file_inspections() def是一种类方法,并且一些var名称对一般受众更有用,否则是逐字的。FileInspection逐字!

def to_file_inspections(data_store)
    jar = open("jar.pkl","wb")
    for f in data_store:
        f_ob = FileInspection(f) #custom class, not very elaborate
        cPickle.dump(jar,f_ob, -1)
    jar.close()
    #now just to test it...
    data = cPickle.load(open("jar.pkl", "rb"))
    print repr(data)

结果:

    Traceback (most recent call last):
      File "main.py", line 400, in <module>
        to_file_inspections()
      File "main.py", line 355, in to_file_inspections
        data = cPickle.load(open("jar.pkl", "rb"))
    EOFError

如果重要,这是我的FileInspection课程:

class FileInspection:
    def __init__(self, path):
        self._path = path.rstrip('\n')
        self._hash = self.hash_file()
        self._inspectable = True
        stats = os.stat(self._path)
        self._size = stats.st_size
        self._last_mod = stats.st_mtime

    def hash_file(self):
        read_size = 1024 # You can make this bigger
        checksum = hashlib.md5()
        with open(self._path) as f:
            data = f.read(read_size)
            while data:
                checksum.update(data)
                data = f.read(read_size)
        checksum = checksum.hexdigest()

        return checksum

    def toString(self):
        return '{0} = {1}"checked":false, "path":"{2}", "hash":{3} "inspectable":{4}, "filesize":{5}, "lastmod":"{6}"{7}'.format(
            self._hash, "{", self._path, self._inspectable, self._hash, self._last_mod, "}\n")

    def toJSON(self):
        return '\t"{0}":{1}\n\t\t"checked":false,\n\t\t"path":"{2}",\n\t\t"hash":"{3}",\n\t\t"inspectable":"{4}",\n\t\t"filesize":"{5}",\n\t\t"last_mod":"{6}"\n\t{7}'.format(
            self._hash, "{", self._path, self._hash, self._inspectable, self._size, self._last_mod, "}")

    @property
    def path(self):
        return self._path

    @property
    def hash(self):
        return self._hash

    @property
    def inspectable(self):
        return self._inspectable

    @property
    def size(self):
        return self._size

    @property
    def last_mod(self):
        return self._last_mod

1 个答案:

答案 0 :(得分:1)

对不起大家。我的循环不起作用的原因是一个非常愚蠢的原因;我对pickle.dump()的参数顺序错误。