22

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

zztop
  • 701
  • 1
  • 7
  • 20

10 Answers10

43

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
korakot
  • 37,818
  • 16
  • 123
  • 144
  • 8
    in colab it gives `cp: cannot open '/content/drive/My Drive/file.gdoc' for reading: Operation not supported` – SantoshGupta7 May 16 '20 at 10:18
  • @SantoshGupta7 Did you ever find a solution to this? I'm trying to backup my files and am running into the same problem. – StarDust Jun 19 '21 at 13:54
  • I don't remember but I remember using google cloud as a workaround for something issues – SantoshGupta7 Jun 28 '21 at 16:24
  • 1
    @SantoshGupta7 That error comes up (for me at least) when the "MyDrive" part of the path is wrong. In my case, I had omitted it, in the error message you posted, it has a space ("My Drive" instead of "MyDrive"). – Adair Sep 15 '21 at 22:08
12

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"
erp_da
  • 199
  • 1
  • 8
5

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

import shutil
shutil.copy("/content/file.doc", "/content/gdrive/file.doc")
Tugay
  • 2,057
  • 5
  • 17
  • 32
Avinash1a
  • 81
  • 1
  • 2
  • This worked perfectly (just have to get the Google drive directory right) and it is way way faster than clicking on the 3 dots next the file in Colab, downloading to local drive and then uploading to Google drive. Thanks!!!! Less than 30 secs for a 3.14gb file. – Arnold Sep 08 '21 at 21:15
  • 1
    I'm getting a OSError: [Errno 95] Operation not supported: '/content/gdrive/file.doc' – pfrank Jan 31 '22 at 16:40
3

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()
1

in my case I use the common approach with the !cp command.

But sometimes, it didn't work in Colab because we didn't enter the right file path.

basic code: !cp source_filepath destination_filepath

implementation code: !cp /content/myfolder/myitem.txt /content/gdrive/MyDrive/mydrivefolder/

in addition, to correctly enter the path, you can copy the path location from the table of contents on the left side by clicking the dot menu -> copy path.

0

Once you see the file in the Table of Contents of Colab on the left, simply drag that file into the "/content/drive/My Drive/" directory located on the same panel. Once the file is inside your "My Drive", you will be able to see it inside your Google Drive.

0

After you mount your drive...

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

...just prepend the full path, including the mounted path (/content/drive) to the file you want to write.

someList = []
with open('/content/drive/My Drive/data/file.txt', 'w', encoding='utf8') as output:
    for line in someList:
        output.write(line + '\n') 

In this case we save it in a folder called data located in the root of your Google Drive.

typhon04
  • 2,350
  • 25
  • 22
0

You may often run into quota limits using the gdown library.

Access denied with the following error:

Too many users have viewed or downloaded this file recently. Please
try accessing the file again later. If the file you are trying to
access is particularly large or is shared with many people, it may
take up to 24 hours to be able to view or download the file. If you
still can't access a file after 24 hours, contact your domain
administrator. 

You may still be able to access the file from the browser:
 https://drive.google.com/uc?id=FILE_ID

No doubt gdown is faster but i copy my files using the command below and avoid quota limits

!cp /content/drive/MyDrive/Dataset/test1.zip /content/dataset
EdgeDev
  • 2,376
  • 2
  • 20
  • 37
0

after get access a Google Drive file from Colab by:

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

you can use this command line to make copy from file or directory

First, navigate to the directory where the file you want to copy is located. You can use the %cd command to change to the directory.

example:

%cd /content/drive/MyDrive/colab_folder

Next, use the !cp command to make a copy of the file. The syntax for the !cp command is:

!cp source_file destination_file

destination_file: the path of the file you want to create as the copy.

example:

!cp file.csv /content/drive/MyDrive/foleder_1

and to move file use :!mv instead of !cp

0

Use wget to download from internet. Put the ! In front to get bash access. You could then use echo, cp, etc. to create files and move them around.

!wget https://raw.githubusercontent.com/werowe/HypatiaAcademy/9aa43bb833a2fcb8ddd10b77fcedd9ed0248795c/assignment/customers.csv

Walker Rowe
  • 953
  • 1
  • 12
  • 24