Why do I get “Pickle - EOFError: Ran out of input” reading an empty file?

前端 未结 7 2441
小鲜肉
小鲜肉 2020-11-28 05:55

I am getting an interesting error while trying to use Unpickler.load(), here is the source code:

open(target, \'a\').close()
scores = {};
with o         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 06:38

    As you see, that's actually a natural error ..

    A typical construct for reading from an Unpickler object would be like this ..

    try:
        data = unpickler.load()
    except EOFError:
        data = list()  # or whatever you want
    

    EOFError is simply raised, because it was reading an empty file, it just meant End of File ..

提交回复
热议问题