1

I wanted to count the number of lists in my binary file for a project that I have made, so that I can print my previously stored data but I receive an EOF error. Can you tell me why?

import pickle
F=open('binary_file_1.dat','rb')
while True:
    List=pickle.load(F)
    l=l+1
F.close()
print('l=',l)

Output:

Traceback (most recent call last):
  File "C:\Users\Jonas\Desktop\program_x.py", line 7, in <module>List=pickle.load(F)
EOFError: Ran out of input

Open to all suggestions. Thank you, readers

4
  • If the serialised object is a Python class, you can introspect dict to determine what types of attributes are contained within it Commented Aug 13, 2021 at 10:37
  • Sorry I didn't get you. Oh actually I was given a list of lists so I don't need to figure out the datatype but thanks. If you know the answer please let me know Commented Aug 13, 2021 at 14:45
  • Can you show what the object looks like that you serialised (pickled). Was it a single class? Commented Aug 14, 2021 at 7:28
  • I also finally devised a way ,my list looked liked this [Milk,2,100,40] which shows the item name, discount and other related numbers. So, to count such lists I made this code count=1 F=open('binary_file_1.dat','rb') Y=F.readlines() for x in Y: z=len(x) for q in range(z-1): if x[q:q+1]==b'.': count=count+1 F.close() Thanks for your concern Commented Aug 14, 2021 at 8:54

1 Answer 1

3

Don't use pickle.load() in for loop. When loading pickled file you get the same object you saved with pickle.dump(), so if your object was for example list of lists, you could use len(pickle.load(f)) to get number of lists, or something similar based on object type.

Sign up to request clarification or add additional context in comments.

2 Comments

So sorry but I I think you misunderstood me, I actually wanted to count the number of lists and not the number of elements of the list. Can you resolve my problem now
Even the cursor doesn't come out of the while loop if it's false in python.3-x, it just shows EOF error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.