Home > Back-end >  Store a dictionary variable from Google Colab locally
Store a dictionary variable from Google Colab locally

Time:01-21

In Google Colab, I have created a dict file names list_dict. How can pickle this and store it locally?

PATH = <LOCAL_PATH>
pickle_out = open(PATH  '<FILE_NAME>.pickle', 'wb')
pickle.dump(list_dict, pickle_out)
pickle_out.close()

However, this returns:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-64-439362c7678e> in <module>
----> 1 pickle_out = open(PATH , 'wb')
      2 pickle.dump(list_dict, pickle_out)
      3 pickle_out.close()

FileNotFoundError: [Errno 2] No such file or directory:

How can I store this variable locally?

CodePudding user response:

The following approach works:

from google.colab import auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
import pickle

with open('data.pickle', 'wb') as handle:
    pickle.dump(list_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)

#Authenticate and construct the Drive API client
auth.authenticate_user()
drive_service = build('drive', 'v3')

file_metadata = {'name': 'data.pickle'}
media = MediaFileUpload('data.pickle', mimetype='application/octet-stream')
file = drive_service.files().create(body=file_metadata, media_body=media,
                                    fields='id').execute()
print(F'File ID: {file.get("id")}')

Then, the file data.pickle is stored in the root directory in Google Drive and can be downloaded from there.

CodePudding user response:

Try using:

PATH = <LOCAL_PATH>
pickle_out = open(PATH  '<FILE_NAME>.pickle', 'wt')
pickle.dump(list_dict, pickle_out)
pickle_out.close()

or

PATH = <LOCAL_PATH>
pickle_out = oprn(PATH  '<FILE_NAME>.pickle', 'w')
pickle.dump(list_dict, pickle_out)
pickle_out.close()
  • Related