Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google colab and google drive: Copy file from colab to Google Drive

There seem to be lots of ways to access a file on Google Drive from Colab but no simple way to save a file from Google Colab back to Google Drive.

For example, to access a Google Drive file from Colab, you can mount the Google Drive using

from google.colab import drive
drive.mount('/content/drive')

However, to save an output file you've generated in Colab on Google Drive the methods seem very complicated as in:

Upload File From Colab to Google Drive Folder

Once Google Drive is mounted, you can even view the drive files in the Table of Contents from Colab. Is there no simple way to save or copy a file created in Colab and visible in the Colab directory back to Google Drive?

Note: I don't want to save it to a local machine using something like

from google.colab import files
files.download('example.txt')

as the file is very large

like image 914
zztop Avatar asked Jan 13 '20 03:01

zztop


4 Answers

After you have mounted the drive, you can just copy it there.

# mount it
from google.colab import drive
drive.mount('/content/drive')
# copy it there
!cp example.txt /content/drive/MyDrive
like image 155
korakot Avatar answered Oct 21 '22 12:10

korakot


You can use shutil to copy/move files between colab and google drive

import shutil
shutil.copy("/content/file.doc", "/content/gdrive/file.doc")
like image 30
Avinash1a Avatar answered Oct 21 '22 12:10

Avinash1a


Other answers suggest how to copy a specific file, I would like to mention you can also copy the entire directory, which is useful when copying logs from callbacks from Colab to Drive:

from google.colab import drive
drive.mount('/content/drive')

In my case, the folder names were:

%cp -av "/content/logs/scalars/20201228-215414" "/content/drive/MyDrive/Colab Notebooks/logs/scalars/manual_add"
like image 29
erp_da Avatar answered Oct 21 '22 12:10

erp_da


When you are saving files, simply specify the Google Drive path for saving the file.

When using large files, Colab sometimes syncs the VM and Drive asynchronously. To force the sync, simply run:

from google.colab import drive
drive.flush_and_unmount()
like image 36
Jitesh Seth Avatar answered Oct 21 '22 10:10

Jitesh Seth