1

Python 2.7.14 |Anaconda, Inc.| IPython 5.4.1

I do not understand why depending on how I give the arguments to pickle, I am getting the desired result or an error Please, take a look to this example:

import urllib
import pickle

p= pickle.load(urllib.urlopen("http://www.pythonchallenge.com/pc/def/banner.p"))

link='http://www.pythonchallenge.com/pc/def/banner.p'
q= pickle.load(urllib.urlopen(link))

and I get the data for p and q (list 23 elements)

However, If I try:

   f = urllib.urlopen(link)   
   r= pickle.load(f)

I get:

raise EOFError

If I try:

myfile = f.read()
s= pickle.load(myfile)

I get:

AttributeError: 'str' object has no attribute 'readline'

I search in SO for similar solutions, but I failed to find one that could give me an answer.

Can anyone help me to understand why r and s failed, when q and f look the same to me?

******EDITED TO RESPONSE DAVIS HERRING ********

import urllib
import pickle

link="http://www.pythonchallenge.com/pc/def/banner.p"
f = urllib.urlopen(link)   
#myfile = f.read()

r= pickle.load(f)

Removing myfile = f.read(), makes r to work. You were right! Now I understand your answer, I cannot read f then use it on load(). However, it looks reading f changes f?

Ok, I found an answer on this topic here

  • 2
    Did you evaluate the assignments to `f` and `r` just once each? – Davis Herring Dec 03 '18 at 22:41
  • yes.. just how appear in my code... not sure if I understood your question thought. –  Dec 05 '18 at 02:42
  • 1
    You can’t pass `f` to `pickle.load` *and* (usefully) call `read` on it. Show *one* example, or else one *complete* example per attempt. – Davis Herring Dec 05 '18 at 03:24
  • Thanks. Now I understood your comment, and you are right. I Edited my answer to show the working code. –  Dec 05 '18 at 13:55

1 Answers1

0

It doesn’t affect pickle.load to attach or not attach a name (f) to the file object returned by urllib.urlopen(link). (Attaching a name does have another advantage, which is that you can close it properly—use with—rather than automatically and unreliably.)

However, what is returned is a stream—a source of data rather than a container of it. It can’t be read twice (or read and also loaded) because it doesn’t store a copy of anything (which can be important for large files/pages).

If you do have the data in a (byte) string, use loads.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76