Tips & Tricks #1 – Files with Google Colab

  • Post category:Blog / python
Tips & Tricks #1 - Files with Google Colab

This article Tips and Tricks #1 is dedicated to Google Colab and the exchange of files between the local Colab machine at Google, your computer, a Github repository and your Google Drive space.

Don’t forget that Python programs made with Colab are executed on a remote machine.
It may therefore be necessary to set up a gateway between the remote machine and/or your computer and/or your Google Drive space to exchange data (data files, images, sounds, database…).

Otherwise, the files generated on Colab will be lost when you close your web browser or if you reset the runtime environment of Colab using the Run menu.

1. Mount your Google Drive in Colab

The code below allows you to access your Google Drive files from Colab.

When you run the code:

  • Colab will display a URL for you to click on,
  • select the Google account you want to access the drive for,
  • confirm the connection,
  • copy the authorization code and close the browser tab,
  • paste in the field “Enter your authorization code”,
  • validate with Enter

After execution, the message “Mounted at /content/drive” will be displayed and you should see the directory “drive” in the file navigator on the left of the notebook.

If not, you may need to click the Refresh button at the top of the file navigator to refresh the list of files.

You can then access all your files and folders.

# Mount your Google Drive
from google.colab import drive
drive.mount('/content/drive')

2. Upload files from your computer to Colab

This allows you to upload one or more files at a time to the local disk of your remote Google Colab machine.

When you run the code:

  • A “Select files” button and “Cancel upload” button will appear,
  • Click on the “Select files” button to open the file selector,
  • Select one or more files and validate,
  • the upload will be indicated with a progress counter,
  • the files will then be visible in the file browser on the left of the notebook.

If not, you may need to click the Refresh button at the top of the file tree to refresh the list.

# Upload file(s) to Colab local disk space
from google.colab import files
uploaded = files.upload()

The list of uploaded files is stored in the variable ‘uploaded‘ which is of type List().

3. Download a file from Colab to your computer

Allows you to download a file from Colab local disk to your computer.

Indicate the full or relative path of your file and execute the code for the download to start.

It is possible, depending on your Internet browser, that a dialog box asks you if you want to authorize the download request.

# Download a file from Colab to your computer
from google.colab import files
files.download('path/to/your/file')

3. Get a GitHub repository content

We will not see the synchronization between Colab and a GitHub repository, but how to get the content of a GitHub repository from Google Colab to install a package for example.

First, if necessary, go to the directory where you want to get the GitHub repository with the command %cd /your/path/

Then, run the code below replacing “link/to/github/repo” with the URL of the repository whose content you want to download.

# Get GitHub repository
!git clone "link/to/github/repo"

In case of a bad URL, you will get the error message “fatal: repository ‘link/to/your/repo’ does not exist”.
Otherwise, you should see the new directory with the contents of the GitHub repository in the file navigator on the left of the notebook.

Again, if not, you may need to click the Refresh button at the top of the file tree to refresh the list.

That’s it for this Tips & Tricks #1, see you on next time đŸ˜‰

All our articles

Article published under CC BY licence

Tips & Tricks #1 - Files with Google Colab
CC BY licence