Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I have generated some data and stored it many times like this:

with open('filename', 'a') as f:

        pickle.dump(data, f)

Every time the size of the file increased, but when I open the file

with open('filename', 'rb') as f:

    x = pickle.load(f)

I can see only information from the last time. How can I correctly read the file?

1 Answer

0 votes
by (108k points)

Please be informed that the Pickle will serialize a particular object at a time, and reads back a single object - the pickled data is recorded in sequence on the file.

If you simply do pickle.load you should be reading the first object serialized into the file.

objects = []

with (open("myfile", "rb")) as openfile:

    while True:

        try:

            objects.append(pickle.load(openfile))

        except EOFError:

            break

 Want to become a Python Developer? Check out this insightful Python Certification course.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 1, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer
asked Feb 9, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Jul 13, 2020 in Python by ashely (50.2k points)

Browse Categories

...