Loading and saving files in Google Colab

2 minute read see also comments

Google Colab is a convenient solution to execute Jupyter notebooks. It runs entirely in the cloud and requires no initial setup. You can even share and edit notebooks in a team. While it supports the most popular Python libraries and Jupyter notebooks run right away just as if they were executed in your local Python installation, it requires a few extra commands to enable the I/O support, e.g., to read data files or to save plots.

img

Loading data files in Google Colab

To load some data from a file (csv, xls, txt, …) in Google Colab, you need to establish the access to your Google Drive. To do so, add the following import and mount commands to your notebook,

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

and upload the corresponding files into your Google Drive. The path to access the uploaded files follows:

"/content/drive/My Drive/PATH/TO/THE/DATAFOLDER/"

Saving files into your Google Drive

For storing data files that you have created yourself into your Google Drive (e.g., Pandas DataFrames as Excel or CSV files, or plots generated with Matplotlib), use again the import and mount command mentioned above,

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

and adjust the path for saving the file accordingly:

"/content/drive/My Drive/PATH/TO/SAVE/THE/PLOT/your_file.csv"

The first time you save something from Google Colab into your Google Drive, you will be prompted to grant permission to access it.

Here is an example script to save a Matplotlib plot:

import numpy as np
import matplotlib.pyplot as plt
from google.colab import drive
drive.mount('/content/drive')

dx = 1
length = 2
x = np.arange(0, length * np.pi, dx)
y = np.sin(x)

plt.plot(x,np.cos(x), label="Cosine", lw=3) 
plt.plot(x,np.sin(x), "--", label="Sine", lw=3) 

plt.xlabel("x (distance in radians)")
plt.ylabel("y (sin(x))")
plt.title("Our first signal")
plt.legend(loc="best")

plt.tight_layout()

plt.savefig("/content/drive/My Drive/your_plot.pdf")

Downloading files to your local disk

You can bypass the storage of files to your Google Drive and download them directly to your local hard drive. Add the following import command to your notebook,

from google.colab import files 

and use the following command after your default file-saving command:

files.download("my_plot.pdf")

Here is an example script for saving a plot generated with Matplotlib:

import numpy as np
import matplotlib.pyplot as plt
from google.colab import files 

dx = 1
length = 2
x = np.arange(0, length * np.pi, dx)
y = np.sin(x)

plt.plot(x,np.cos(x), label="Cosine", lw=3) 
plt.plot(x,np.sin(x), "--", label="Sine", lw=3) 

plt.xlabel("x (distance in radians)")
plt.ylabel("y (sin(x))")
plt.title("Our first signal")
plt.legend(loc="best")

plt.tight_layout()

plt.savefig("my_plot.pdf", dpi=120)
files.download("my_plot.pdf")

And to save a Pandas DataFrame:

import numpy as np
import pandas as pd
from google.colab import files 

my_data = pd.DataFrame(data=np.random.random((4,4)))
my_data.to_csv("my_data.csv")
files.download("my_data.csv")

comments