【问题标题】:Python why binary file(empty) is not being dumpedPython为什么没有转储二进制文件(空)
【发布时间】:2020-12-06 07:37:42
【问题描述】:

这是我正在处理的代码...

这里 Name.dat 是空文件...

# Library Management System
import time
import pickle
import csv
print("Welcome to the Library Management System")
time.sleep(0.3)
print("Hello there",end="")
print(".",end="")
time.sleep(0.5)
print(".",end="")
time.sleep(0.5)
print(".")
time.sleep(0.5)
while True:
    try:
        print("If you are new user press 1 to continue::")
        print("If you are an existing user press 2 to continue::")
        print("If you want to exit press 3::")
        time.sleep(0.5)
        n=int(input("Enter your choice::"))
    except:
        print("Only integer values.")
    try:
        if n==1:
            print("here")
            name_list=[]
            print("here")
            f=open("Name.dat","rb+")
            print("here")
            s=pickle.load(f)
            print("here")
            if len(s)==0:
                pass
            else:
                for i in s:
                    name_list.append(i)
            f=open("Name.dat","ab+")
            l=[]
            user=input("Enter username::")
            while True:
                truth=1
                if len(name_list)==0:
                    pass
                else:
                    for i in range(len(name_list)):
                        if user==name_list[i][0]:
                            truth=0
                if truth==1:
                    break
                user=input("Enter username::")
            l.append(user)
            ps=input("Enter password::")
            l.append(ps)
            l.append([])
            pickle.dump(l,f)
            f.close()
            print("Your account has been successfully made.")
        elif n==2:
            f=open("Name.dat","rb+")
            b=int(input("Enter the password::"))
            try:
                while True:
                    s=pickle.load(f)
                    if s[1]==b:
                        print("Hello",s[0])
                        print("What do you want to do?")
                        print("Enter 1 to borrow a book::")
                        print("Enter 2 to return a book::")                        
            except EOFError:
                f.close()
        elif n==3:
            f=open("csv_file.csv",'r')
            csvr=csv.reader(f)
            for line in csvr:
                #copying data into a temporary storage area from csv file
                print(line)
            f.close()    
            break
        elif n>3:
            print("Wrong input")
    except IOError:
        print("swomething")
        None
      

当我在python shell中输入1时出现问题...

Welcome to the Library Management System
Hello there...
If you are new user press 1 to continue::
If you are an existing user press 2 to continue::
If you want to exit press 3::
Enter your choice::1
here
here
here
Traceback (most recent call last):
  File "C:\Users\CCFFIN\AppData\Local\Programs\Python\Python38\Python big big c project\LBS.py", line 30, in <module>
    s=pickle.load(f)
EOFError: Ran out of input   

错误出现在 pickle .load 中,但我不知道为什么会这样

是因为 Name.dat 是空的吗?

我基本上想要的是这样的东西,没有任何例外

Welcome to the Library Management System
Hello there...
If you are new user press 1 to continue::
If you are an existing user press 2 to continue::
If you want to exit press 3::
Enter your choice::1
here
here
here
here 
Enter username::

请注意,我在这里打印是为了识别错误,因此没有必要

csv_file 不为空,里面有 51 个列表

还请说明为什么转储不起作用以及为什么会出现 EofError

提前谢谢你

【问题讨论】:

  • 欢迎来到 Stack Overflow。请阅读how to ask a good question,因为您的问题存在许多问题。就个人而言,我认为它很长(尽管并不总是很糟糕)。但是你一次问多个问题,这是不允许的。而且您的代码比必要的要长得多。请专注于一个问题并为它创建一个minimal reproducible example。如果您的代码导致错误,请包含完整的错误跟踪。那时你可能会得到更好的回应。祝你好运!
  • 关于你的问题的内容:想要达到什么目的?为什么使用pickle?这对我来说似乎很奇怪,但我不知道你的意图。无论如何,如果你真的应该使用pickle来读取文件,那么文件不应该是空的。
  • 好的先生,我会从下一个问题中解决这个问题
  • 谢谢,但实际上你也应该尝试改进这个问题:-)

标签: python file binary


【解决方案1】:

首先我尝试这样做:

        f=open("Name.dat","rb+")
        print("here")
        s=pickle.load(f)
        print("here")
        if len(s)==0:
            pass
        else:
            for i in s:
                name_list.append(i)

我试图把上面的代码改成这个:

    print("here")
    f=open("Name.dat","rb+")
    print("here")
    if len(f.read())==0:
        pass
    else:
        s=pickle.load(f)
        for i in s:
            name_list.append(i)

当我在 bin 文件中的值中转储(Name.dat 中没有其他值)时,它第一次起作用,但第二次错误再次出现(EOFError)。

所以我这样做了(更改了上面的代码)并修复了整个问题:

        if len(f.read())==0:
            pass
        else:
            try:
                s=pickle.load(f)
                for i in s:
                    name_list.append(i)
            except EOFError:
                f.close()

我在 Stack Overflow 上阅读了一篇关于同一问题但未正确回答的文章:EOFError: Ran out of input

实际上要避免二进制文件中出现EOFError,您必须始终使用此方法

try:
    x=pickle.load(File))
except EOFError:
    break

请注意,这两个步骤对我上面的代码都很重要。

【讨论】:

  • 答案应该提供问题的答案。在找到解决方案之前,您不必包括您尝试的所有步骤。我认为这只会使其他人感到困惑,并且会使您的回答超出必要的时间。您也不必向版主致辞或感谢任何人。本网站的目的是收集问题和答案。如果问题或答案有用,您可以点赞。在问答中不需要说“谢谢”(尽管在许多情况下这是礼貌的)。见how do I write a good answer
  • 主题反馈:EOFError 通常不会出现(除非您的文件已损坏)并且不应以这种方式处理。但是正如我在对您的问题的评论中所写的那样,我想知道pickle 是否真的是您在这里需要的。但如果没有更多上下文,我无法确定。
  • PS:您提到的另一个 SO 问题现在正确地是 answered(至少在我看来)。也许这个答案对你也有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 2020-09-25
  • 2019-08-31
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多