LoginSignup
5
5

More than 5 years have passed since last update.

Google ColabolatoryでGoogle Driveのファイルを読み込んでPythonで操作

Posted at

参考:google Colaboratoryでファイルを読み込む方法

drive内のディレクトリをマウントする

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

これを実行すると認証を求められるのでURLに飛んでコピペして認証

Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth...
  Enter your authorization code:
  ··········
Mounted at /content/gdrive

マウントされていることの確認

gdriveのMy Drive以下にdriveのルートディレクトリがマウントされる

!ls ./gdrive/'My Drive'/'Colab Notebooks'

マウントしたGoogle DriveのフォルダをPythonから操作

import os

base_dir = '/content/gdrive/My Drive/Colab Notebooks'
os.listdir(base_dir)

以下のようにフォルダが出力される

['Folder1', 'Folder2', 'File1', 'File2']
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5