Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export non txt data from Colaboratory to Google Drive?

I'm running Colab and I want to save some non-txt data (numpy arrays, PIL images, .h5 keras/tensorflow models) to my drive.

I'm able to save .txt files using this script

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default() 
drive = GoogleDrive(gauth)

# PyDrive reference:
# https://googledrive.github.io/PyDrive/docs/build/html/index.html

# 2. Create & upload a file text file.
uploaded = drive.CreateFile({'title': 'Sample upload.txt'})
uploaded.SetContentString('Sample upload file content')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

# 3. Load a file by ID and print its contents.
downloaded = drive.CreateFile({'id': uploaded.get('id')})
print('Downloaded content "{}"'.format(downloaded.GetContentString()))

but I can't using it for other types of data.

Any help would be very much appreciated!

like image 703
Ahmed Besbes Avatar asked Jan 01 '26 09:01

Ahmed Besbes


1 Answers

pydrive supports uploading a file as well as a string -- see this sample in the docs.

In addition, you can also set the MIME type when creating the file, eg

uploaded = drive.CreateFile({'title': 'sample.csv', 'mimeType': 'text/csv'})
like image 163
Craig Citro Avatar answered Jan 06 '26 11:01

Craig Citro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!