Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

15
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Colaboratoryを使う際のGoogle Driveマウント方法など取り扱いかた色々

Posted at

はじめに

対象読者

  • Colaboratoryを使って、継続的にデータを残したい人

このページの目的

  • Colaboratoryを使って色々動かすときに、ファイルを毎回アップロードする手間を省く

その他

アジェンダ

1.ColaboratoryでのGoogleDriveマウント方法
2.GoogleDriveの取り扱い方いろいろ
3.終わりに

1.ColaboratoryでのGoogleDriveマウント方法

以下のコードを実行すると、MyDrive直下がマウントされます。

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

マウントされると、左メニューの「ファイル」タブに"drive"という文字が現れます。
bandicam 2018-11-08 08-17-24-684.jpg

これで、いちいちファイルをアップロードしなくても、GoogleDriveからデータを読み込んだり、逆に書き込みを行ったりすることができます。

2.GoogleDriveの取り扱い方いろいろ

基本的な使い方は①の通りです。
そのほかに試したことなどを追記していきたいと思います。

どのようにアクセスするのか?

MyDrive直下までのパスは「/content/drive/My Drive/」です。
私は、Colaboratoryようにdatasというフォルダを作成しているので、下記のようにbase_dirを作成しています。

base_dir = "/content/drive/My Drive/datas/".replace('/', os.sep)

ファイルの書き込み方法

例えば、OpenCVでの保存方法は下記のようになります。
普通にローカルで保存するのと同じですね。

cv2.imwrite(base_dir + "images/im.png", resultImage)

3.終わりに

ColaboratoryとGoogle Driveの連携で、めちゃくちゃ便利に検証環境が作成できました。
本当に感謝しかない。

まだまだ使い倒しているわけではないので、いろいろ調べながら更新していきたいと思います。

15
19
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
15
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?