0

Here is the code I am working on...

Here Name.dat is empty file...

# 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
      

The problem occurs when I enter 1 in the python shell...

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   

The error is in pickle .load but I have no idea why that is happening

is it because Name.dat is empty?

What i basically want is something like this without any exceptions

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::

Ps note that I printed here to identify the error so that is not necessary

csv_file is not empty and has 51 lists in it

also please tell why dump is not working and why is there an EofError

Thank you in advance

  • 1
    Welcome to Stack Overflow. Please read [how to ask a good question](https://stackoverflow.com/help/how-to-ask) because there are a number of problems with your question. Personally, I think it's quite long (although that does not always have to be bad). But you're asking multiple questions at once, which is not allowed. And your code is much longer than necessary. Please focus on ONE question and create a [mre] for it. If your code results in an error, include the complete error trace. You will probably get better responses then. Good luck! – wovano Aug 17 '20 at 17:58
  • 1
    Regarding the content of your question: what are trying to achieve? Why do you use `pickle`? It seems very strange to me, but I don't know your intentions. Anyway, if you really should use pickle to read a file, the file should not be empty. – wovano Aug 17 '20 at 18:02
  • Ok sir I will fix that from my next questions – RUPANUGA P MISHRA Aug 18 '20 at 02:34
  • Thanks, but you should try to improve this question as well actually :-) – wovano Aug 18 '20 at 05:42

1 Answers1

0

First I tried to do this:

        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)

I tried to change the code from above to this one:

    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)

It worked for the first time when I was dumping (There was no other value in Name.dat) in the value in bin file but the second time the error came again (EOFError).

So I did this (changed the above code) and this fixed the whole thing:

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

I read an article in Stack Overflow which was on this same question but was not answered properly: EOFError: Ran out of input .

Actually to avoid EOFError in binary files you have to always use this method

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

Note that both the steps are important for my code above.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • An answer should provide the answer to the question. You don't have to include all steps you attempted before finding the solution. I think this will only confuse others and it makes your answer longer than necessary. You also don't have to address the moderators or to thank anybody. The purpose of this site is to gather Questions and Answers. If a question or answer is useful, you can upvote it. Saying "thank you" (although that's polite in many circumstances) is not necessary in questions and answers. See [how do I write a good answer](https://stackoverflow.com/help/how-to-answer). – wovano Aug 17 '20 at 18:18
  • On-topic feedback: an `EOFError` should normally not occur (unless your file is corrupted) and should not be handled this way. But as I wrote in a comment on your question, I wonder if `pickle` is really what you need here. But I can't know for sure without more context. – wovano Aug 17 '20 at 18:20
  • 1
    PS: The other SO question that you mentioned is now properly [answered](https://stackoverflow.com/a/63457062/10669875) (at least in my opinion). Maybe that answer is helpful for you as well. – wovano Aug 17 '20 at 19:16