简体   繁体   English

如何通过 Google Colab notebook (.ipynb) 访问 Google Drive 上的文件?

[英]How to access files on Google Drive via Google Colab notebook (.ipynb)?

I have uploaded all the files I need to train (fine-tune) an object detection model on my Google Drive account.我已经上传了在我的 Google Drive 帐户上训练(微调)对象检测模型所需的所有文件。 There, I have opened an ipynb file via google colab and I need to run some scripts to initialize things and run training.在那里,我通过 google colab 打开了一个 ipynb 文件,我需要运行一些脚本来初始化和运行训练。

But I ran into a problem of not knowing how to access the files that I uploaded from the notebook.但是我遇到了一个问题,不知道如何访问我从笔记本上传的文件。 The notebook is created in the same directory of the scripts that I need to run.笔记本是在我需要运行的脚本的同一目录中创建的。

When I execute !ls I get only one datalab folder in return, and !pwd returns /content .当我执行!ls我只得到一个datalab文件夹作为回报,而!pwd返回/content

I want to know if there's a way to access all the files I have uploaded to my google drive account.我想知道是否有办法访问我上传到我的谷歌驱动器帐户的所有文件。 I may be doing the wrong thing trying to train a model this way, but I don't know any other.我可能在尝试以这种方式训练模型时做错了事情,但我不知道其他任何事情。 So, please help :).所以,请帮忙:)。

Thank you in advance.先感谢您。

Here what I use to download a file from Google Drive to Google Colab file system, substitute fileId value with your Drive file id.在这里,我用来将文件从 Google Drive 下载到 Google Colab 文件系统,将fileId值替换为您的 Drive 文件 ID。

!pip install -U -q PyDrive

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

fileId = '1234567890dcz4qL-JQtYqX9ZXUt7JXe'
fileName = fileId
downloaded = drive.CreateFile({'id': fileId})
downloaded.GetContentFile(fileName)

To get Drive file id look at this answer .要获取 Drive 文件 ID,请查看此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM