text 带有Colaboratory Jupyter笔记本的Google Drive文件I / O.

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 带有Colaboratory Jupyter笔记本的Google Drive文件I / O.相关的知识,希望对你有一定的参考价值。

# Google Drive file I/O with Colab Jupyter notebooks
A minimal working example.

## Instructions

1. Place the Jupyter notebook in your Google Drive.
1. In Google Drive in your web browser, open the Jupyter notebook with Colaboratory.
1. 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/`.
{
  "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

以上是关于text 带有Colaboratory Jupyter笔记本的Google Drive文件I / O.的主要内容,如果未能解决你的问题,请参考以下文章

将包含约 20K 图像的文件夹添加到 Google Colaboratory

如何在 Colaboratory 中使用 Tensorboard

如何下载在 Colaboratory 工作区中创建的文件?

如何修复 Colaboratory 中的 zipfile 读取错误?

在 colaboratory 中从驱动器加载 xlsx 文件

使用本地 GPU 的 Google Colaboratory 本地运行时