Skip to content

Instantly share code, notes, and snippets.

@kelly-sovacool
Last active February 24, 2023 11:19
Show Gist options
  • Save kelly-sovacool/c33b64b70164be37d75bd40c9b56b498 to your computer and use it in GitHub Desktop.
Save kelly-sovacool/c33b64b70164be37d75bd40c9b56b498 to your computer and use it in GitHub Desktop.
Google Drive file I/O with Colab Jupyter notebooks

Google Drive file I/O with Colab Jupyter notebooks

A minimal working example.

Instructions

  1. Place the Jupyter notebook in your Google Drive.
  2. In Google Drive in your web browser, open the Jupyter notebook with Colaboratory.
  3. Run the cells in this notebook to mount the drive and practice reading & writing files.

Notes

  • In this example, input.txt is a file in the top-level directory of my Google Drive.
  • You can append sub-directories to the path as needed. E.g. to access a file in a directory called my_project, use the path /content/gdrive/My Drive/my_project/.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "fileIO.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kelly-sovacool/c33b64b70164be37d75bd40c9b56b498/fileio.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "T9beHHWTa3Bi",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# Google Drive file I/O with Google Colaboratory\n",
"\n",
"Instructions for accessing external data: https://colab.research.google.com/notebooks/io.ipynb#scrollTo=RWSJpsyKqHjH\n"
]
},
{
"metadata": {
"id": "bZSx-Wviar8P",
"colab_type": "code",
"outputId": "831e90d1-027d-4500-9184-417f054f7497",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 122
}
},
"cell_type": "code",
"source": [
"# mount Google Drive\n",
"from google.colab import drive\n",
"drive.mount('/content/gdrive')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code\n",
"\n",
"Enter your authorization code:\n",
"··········\n",
"Mounted at /content/gdrive\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "aqE3_P9JbM8A",
"colab_type": "code",
"outputId": "189a9aa9-5c6c-4b31-a609-9638dcde01fb",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
}
},
"cell_type": "code",
"source": [
"# read a file\n",
"gdrive_path = '/content/gdrive/My Drive/'\n",
"with open(f'{gdrive_path}input.txt', 'r') as file:\n",
" for line in file:\n",
" print(line)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"testing google colab file reading\n",
"\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "a4CZ84vvbRcJ",
"colab_type": "code",
"outputId": "4d1951b0-8309-47a0-d6e5-3568be72c163",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"# write a file\n",
"with open(f'{gdrive_path}output.txt', 'w') as file:\n",
" file.write('Hello Google Drive!')\n",
"!cat /content/gdrive/My\\ Drive/output.txt"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Hello Google Drive!"
],
"name": "stdout"
}
]
}
]
}
testing google colab file reading
Copy link

ghost commented Dec 10, 2022

is there any way pass oath2 auth. without user interaction?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment