Colaboratory(以下Colab)にてGoogleドライブのマウント方法について紹介します。
今回はColabでGoogleドライブをマウントする方法として以下2種類の方法を紹介します。
- GUIでのマウント
- from google.colab import driveを実行してマウント
Colabのインストールがまだできていない場合は以下を参考にしてください。
GUIでのGoogleドライブマウント方法
Colabにて[ファイル]アイコン>[ドライブをマウント]アイコンをクリックします。
![Colaboにて[ファイル]アイコン>[ドライブをマウント]アイコンをクリックします。](https://www.teamxeppet.com/memorandum/wp-content/uploads/2021/07/image-9.png)
[Google ドライブに接続]をクリックします。

アカウントの選択画面に遷移するので、ドライブをマウントしたいアカウントをクリックします。

Colabの画面に戻り左下にドライブをマウントしている旨、メッセージが表示されるので待ちます。

Googleドライブのフォルダが表示されることを確認します。

試しにGoogleドライブにあるCSVファイルをPandasに読み込ませてみます。
Googleドライブのパスは以下のようにファイルを右クリックし、[パスをコピー]をクリックし取得してみましょう。

PandasにCSVファイルを読み込ませて出力させます。
~サンプル~
import pandas as pd
pd.read_csv(“/content/drive/MyDrive/test/emp.csv”, encoding=”cp932″)

from google.colab import driveを実行してマウント
以下コードを入力し、[Ctrl]+[Enter]を押下します。
from google.colab import drive
drive.mount(‘/content/drive’)
出力結果にある「Go to this URL is a browser:」のURLをクリックします。


アカウントの選択画面に遷移するので、ドライブをマウントしたいアカウントをクリックします。

認証コードが表示されるのでコピーします。

認証コードを[Enter your authorization code:]にペーストし、[Ctrl]+[Enter]を押下します。

出力結果に「Mounted at /content/drive」と表示されることを確認します。

先ほどと同様にGoogleドライブ上にあるCSVファイルをPandasに読み込ませられるか確認します。
~サンプル~
import pandas as pd
pd.read_csv(“/content/drive/MyDrive/test/emp.csv”, encoding=”cp932″)

コメント