Python 脚本中的 EOF 错误

发布于 2024-07-16 07:24:17 字数 1420 浏览 7 评论 0原文

我有以下代码片段:

def database(self):
    databasename=""
    host=""
    user=""
    password=""
    try:
        self.fp=file("detailing.dat","rb")
    except IOError:
        self.fp=file("detailing.dat","wb")
        pickle.dump([databasename,host,user,password],self.fp,-1)
        self.fp.close()
        selffp=file("detailing.dat","rb")
        [databasename,host,user,password]=pickle.load(self.fp)

    return

它有错误:

Traceback (most recent call last):
  File "detailing.py", line 91, in ?
    app=myApp()
  File "detailing.py", line 20, in __init__
    wx.App.__init__(self,redirect,filename,useBestVisual,clearSigInt)
  File "/usr/lib64/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_core.py", line 7473, in __init__
    self._BootstrapApp()
  File "/usr/lib64/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_core.py", line 7125, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "detailing.py", line 33, in OnInit
    self.database()
  File "detailing.py", line 87, in database
    [databasename,host,user,password]=pickle.load(self.fp)
  File "/usr/lib64/python2.4/pickle.py", line 1390, in load
    return Unpickler(file).load()
  File "/usr/lib64/python2.4/pickle.py", line 872, in load
    dispatch[key](self)
  File "/usr/lib64/python2.4/pickle.py", line 894, in load_eof
    raise EOFError
EOFError

我做错了什么?

I have the following code fragment:

def database(self):
    databasename=""
    host=""
    user=""
    password=""
    try:
        self.fp=file("detailing.dat","rb")
    except IOError:
        self.fp=file("detailing.dat","wb")
        pickle.dump([databasename,host,user,password],self.fp,-1)
        self.fp.close()
        selffp=file("detailing.dat","rb")
        [databasename,host,user,password]=pickle.load(self.fp)

    return

It has the error:

Traceback (most recent call last):
  File "detailing.py", line 91, in ?
    app=myApp()
  File "detailing.py", line 20, in __init__
    wx.App.__init__(self,redirect,filename,useBestVisual,clearSigInt)
  File "/usr/lib64/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_core.py", line 7473, in __init__
    self._BootstrapApp()
  File "/usr/lib64/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_core.py", line 7125, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "detailing.py", line 33, in OnInit
    self.database()
  File "detailing.py", line 87, in database
    [databasename,host,user,password]=pickle.load(self.fp)
  File "/usr/lib64/python2.4/pickle.py", line 1390, in load
    return Unpickler(file).load()
  File "/usr/lib64/python2.4/pickle.py", line 872, in load
    dispatch[key](self)
  File "/usr/lib64/python2.4/pickle.py", line 894, in load_eof
    raise EOFError
EOFError

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑丑阿 2024-07-23 07:24:17

除非您有拼写错误,否则问题可能出在将文件句柄分配给 selffp 而不是 self.fp 的这一行:

selffp=file("detailing.dat","rb")

如果这是一个拼写错误,并且您的代码实际上将文件打开到 self.fp,那么您可能希望验证该文件是否确实具有内容(即:之前的 pickle 是否有效)...该错误表明该文件为空。

编辑:在这个答案的评论中,S. Lott 有一个很好的总结,说明了为什么拼写错误会生成您看到的错误,我将其粘贴到此处以确保答案的完整性:“selffp 将是未使用的打开文件,而 self.fp (旧的关闭文件)将用于加载”。

Unless you've got a typo, the issue may be in this line where you assign the file handle to selffp not self.fp:

selffp=file("detailing.dat","rb")

If that is a typo, and your code actually opens the file to self.fp, then you may wish to verify that the file actually has contents (ie: that the previous pickle worked)... the error suggests that the file is empty.

Edit: In the comments to this answer, S. Lott has a nice summary of why the typo generated the error you saw that I'm pasting here for completeness of the answer: "selffp will be the unused opened file, and self.fp (the old closed file) will be used for the load".

奶气 2024-07-23 07:24:17

这是我建议使用的版本:

def database(self):
    databasename=""
    host=""
    user=""
    password=""
    try:
        self.fp=open("detailing.dat","rb")
    except IOError:
        with open("detailing.dat", "wb") as fp:
            pickle.dump([databasename,host,user,password],fp,-1)

        self.fp=open("detailing.dat","rb")
        [databasename,host,user,password]=pickle.load(self.fp)

    return

正如已经指出的,self.fp 上有一个拼写错误。 但我注意到其他一些可能会导致问题的事情。

首先,您不应直接使用文件构造函数。 您应该使用内置的 open 函数。

其次,您应该避免在finally 块之外调用文件的close 方法。 在本例中,我使用了 python 2.6 的 with block。 您可以在 Python 2.5 中通过以下命令使用它:

from __future__ import with_statement

这将防止在任何地方抛出异常时文件被卡在打开状态(因为退出 with 块时它将关闭文件)。 尽管这不是问题的原因,但要记住这一点很重要,因为如果文件对象的方法之一引发异常,则文件将在 sys.traceback 中无限期地保持打开状态。

(请注意,您可能应该接受 Jarret Hardie 的回答,他抓住了这个错误:-))

Here's the version that I would recommend using:

def database(self):
    databasename=""
    host=""
    user=""
    password=""
    try:
        self.fp=open("detailing.dat","rb")
    except IOError:
        with open("detailing.dat", "wb") as fp:
            pickle.dump([databasename,host,user,password],fp,-1)

        self.fp=open("detailing.dat","rb")
        [databasename,host,user,password]=pickle.load(self.fp)

    return

As has been pointed out, there was a typo on self.fp. But here are a few other things that I notice that can cause problems.

First of all, you shouldn't be using the file constructor directly. You should instead use the built-in open function.

Secondly, you should avoid calling a file's close method outside a finally block. In this case, I've used python 2.6's with block. You can use this in Python 2.5 with the following command:

from __future__ import with_statement

This will prevent the file from being stuck open if an exception is thrown anywhere (as it will close the file when the with block is exited). Although this isn't the cause of your problem, it is an important thing to remember because if one of the file object's methods throws an exception, the file will get held open in sys.traceback indefinitely.

(note that you should probably accept Jarret Hardie's answer though, he caught the bug :-) )

魂归处 2024-07-23 07:24:17

当我没有选择正确的模式来读取文件时(wb 而不是 rb),我收到了此错误。 改回 rb 不足以解决问题。 然而,再次生成一个新的干净的pickle文件解决了这个问题。 似乎没有选择正确的模式来打开二进制文件会以某种方式“损坏”该文件,然后该文件将无法打开。

但我是 Python 的初学者,所以我可能也错过了一些东西。

I got this error when I didn't chose the correct mode to read the file (wb instead of rb). Changing back to rb was not sufficient to solve the issue. However, generating again a new clean pickle file solved the issue. It seems that not choosing the correct mode to open the binary file somehow "damages" the file which is then not openable whatsoever afterward.

But I am quite a beginner with Python so I may have miss something too.

墨小墨 2024-07-23 07:24:17

虽然这不是OP问题的直接答案——当我尝试使用以下命令解开二进制文件时,在搜索EOFError的原因时偶然发现了这个答案:pickle.load(open (文件名,“r”))

import cPickle as pickle 
A = dict((v, i) for i, v in enumerate(words))
with open("words.pkl", "wb") as f:
    pickle.dump(A, f)



#...later open the file -- mistake:trying to read a binary with non-binary method
with open("words.pkl", "r") as f:
    A =pickle.load(f) # EOFError


# change that to
with open ("words.pkl", "rb") as f: # notice the "rb" instead of "r"
    A = pickle.load(f)

While this is not a direct answer to the OP's question -- I happened upon this answer while searching for a reason for an EOFError when trying to unpickle a binary file with : pickle.load(open(filename, "r")).

import cPickle as pickle 
A = dict((v, i) for i, v in enumerate(words))
with open("words.pkl", "wb") as f:
    pickle.dump(A, f)



#...later open the file -- mistake:trying to read a binary with non-binary method
with open("words.pkl", "r") as f:
    A =pickle.load(f) # EOFError


# change that to
with open ("words.pkl", "rb") as f: # notice the "rb" instead of "r"
    A = pickle.load(f)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文