Kevin
Kevin

Reputation: 597

Python read multiple pickled dictionary

I have 261 pickled dictionary objects ( task_0.pkl, task_1.pkl ,...)

I want read them. I have tried

c=[]
for i in range(261):
    with open('result_20140213/task_%i.pkl' %i , 'rb') as handle:
        c.append(pickle.load(handle))

It gives

---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-26-a5aa784bbbb0> in <module>()
      2 for i in range(261):
      3     with open('result_20140213/task_%i.pkl' %i , 'rb') as handle:
----> 4         c.append(pickle.load(handle))

EOFError: Ran out of input

Further I tried:

for i in range(261):
    with open('result_20140213/task_%i.pkl' %i , 'rb') as handle:
        pickle.load(handle)

It also gives

---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-28-660cce2aef10> in <module>()
      2 for i in range(261):
      3     with open('result_20140213/task_%i.pkl' %i , 'rb') as handle:
----> 4         pickle.load(handle)

EOFError: Ran out of input

It seems with open can not be used here.

How could I read these pickled dictionaries?

Upvotes: 0

Views: 54

Answers (3)

Kevin
Kevin

Reputation: 597

I know why.... I put 'wb'

for i in range(261):
    with open('result_20140213/task_%i.pkl' %i , 'rb') as handle:
        pickle.load(handle)

So it will firstly create empty files..... Thanks everyone.

Upvotes: 0

rahul
rahul

Reputation: 21

Check whether the file is empty.

    import os
    import io
    for i in range(261):
        with io.TextIOWrapper(open('result_20140213/task_%i.pkl' %i, 'rb')) as handle:
            if os.stat('result_20140213/task_%i.pkl' %i).st_size == 0:
                pickle.load(handle)
`

Upvotes: 1

John Steill
John Steill

Reputation: 1

range(261) yields 0,1,...,260.

Do you have a task_0.pkl?

maybe you meant range(1,262)?

Upvotes: 0

Related Questions