python - 加载pickled数据时cPickle EOFError

标签 python macos python-2.7 pickle

编辑:我很蠢

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

操作

第一次 pickle 物体,抱歉,如果这是一个棘手的问题;看过各种其他 EOFError 帖子,但没有一个有效或似乎适用。

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

我使用的是 OS X (Mavericks);这是我的代码(为了可读性而稍微减少了;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

最佳答案

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

关于python - 加载pickled数据时cPickle EOFError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25837056/

相关文章:

ruby-on-rails - 由于无效选项 : --include-dependencies,Rails 安装失败

python - 如何在 python 中使用 linux 命令遍历 .tar.gz 文件列表

python-2.7 - Python 2.7 默认情况下是否将错误写入日志文件?

python - 替换 IPython 菜单图标

python - 从宽到长返回空输出 - Python 数据框

javascript - Python/Javascript 数字精度

python - Pyautogui 无法在 Mac 上运行? (卡特琳娜)

html - getContext ('2d' ) 在 Safari 10 中返回 null

python - 如何在 pexpect 的日志文件中获取时间戳

python - 获取 dateutil.parse 中的格式