7

The normal way to pickle and unpickle an object is as follows:

Pickle an object:

import cloudpickle as cp

cp.dump(objects, open("picklefile.pkl", 'wb'))

UnPickle an object: (load the pickled file):

loaded_pickle_object = cp.load(open("picklefile.pkl", 'rb'))

Now, what if the pickled object is hosted in a server, for example a google drive: I am not able to unpickle the object if I directly provide the URL of that object in the path. The following is not working:I get an IOERROR

UnPickle an object: (load the pickled file):

loaded_pickle_object = cp.load(open("https://drive.google.com/file/d/pickled_file", 'rb')) 

Can someone tell me how to load a pickled file into python that is hosted in a web URL?

Eypros
  • 5,370
  • 6
  • 42
  • 75
Karthick Mohanraj
  • 1,565
  • 2
  • 13
  • 28
  • Use this link to get an overview: https://stackoverflow.com/questions/1393324/in-python-given-a-url-to-a-text-file-what-is-the-simplest-way-to-read-the-cont – Sumit S Chawla May 31 '18 at 12:21
  • BTW it is not the normal way to use `open()` this way as it is impossible to close the file if you don't assign it to a name. – BlackJack Jun 12 '18 at 14:30

1 Answers1

8

The following has worked for me when importing gdrive pickled files into a Python 3 colab:

from urllib.request import urlopen
loaded_pickle_object = cp.load(urlopen("https://drive.google.com/file/d/pickled_file", 'rb')) 
G Pidcock
  • 96
  • 1
  • 4