首页 > 解决方案 > 如何在 Google Drive 中搜索 TensorFlow 文件?

问题描述

我在这里关注文档:https ://colab.research.google.com/github/google/earthengine-api/blob/master/python/examples/ipynb/TF_demo1_keras.ipynb#scrollTo=43-c0JNFI_m6了解如何将 TensorFlow 与 GEE 一起使用。本教程的一部分是检查导出文件的存在。在文档中,示例代码是:

fileNameSuffix = '.tfrecord.gz'
trainFilePath = 'gs://' + outputBucket + '/' + trainFilePrefix + fileNameSuffix
testFilePath = 'gs://' + outputBucket + '/' + testFilePrefix + fileNameSuffix

print('Found training file.' if tf.gfile.Exists(trainFilePath) 
    else 'No training file found.')
print('Found testing file.' if tf.gfile.Exists(testFilePath) 
    else 'No testing file found.')

就我而言,我只是将文件导出到 Google Drive 而不是 Google Cloud 存储桶。我将如何更改trainFilePathtestFilePath指向 Google Drive 文件夹?FWIW,当我进入 Google Drive 文件夹时,我确实看到了文件。

标签: python-3.xtensorflowgoogle-drive-api

解决方案


解决方案

您可以使用强大的PyDrive库从 Google Collab 轻松访问您的 Drive 文件,从而检查您拥有或已导出的文件等。

以下代码是一个示例,它列出了您的 Google Drive API 根目录中的所有文件。这已在此答案中找到 (是的,我正在将此答案作为社区 wiki 帖子)

# Install the library
!pip install -U -q PyDrive
# Install the rest of the services/libraries needed
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# choose a local (colab) directory to store the data.
local_download_path = os.path.expanduser('~/data')
try:
  os.makedirs(local_download_path)
except: pass

# 2. Auto-iterate using the query syntax, in this case as I am using the main directory of Drive this would be root
#    https://developers.google.com/drive/v2/web/search-parameters
file_list = drive.ListFile(
    {'q': "'root' in parents"}).GetList()

for f in file_list:
  # 3. Print the name and id of the files
  print('title: %s, id: %s' % (f['title'], f['id']))

注意:当您执行此操作时,colab 将带您到另一个页面进行身份验证并让您插入密钥。只需按照服务指示您执行的操作即可,这非常简单。

我希望这对你有所帮助。让我知道您是否需要其他任何内容,或者您​​是否不理解某些内容。:)


推荐阅读