如何在Google云端硬盘中搜索Tensorflow文件?

时间:2020-02-25 19:19:13

标签: python-3.x tensorflow google-drive-api

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

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云端硬盘而不是Google Cloud存储桶。如何更改trainFilePathtestFilePath指向Google云端硬盘文件夹? FWIW,当我进入Google云端硬盘文件夹时,实际上可以看到文件。

2 个答案:

答案 0 :(得分:0)

我想说,您可以使用Google Drive API列出Google Drive中的文件,而不是GCS Bucket。您可以找到文档here

您也可以使用PyDrive,这很容易理解。这是一个示例,您只需要根据需要调整查询“ q”即可:

from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth

gauth = GoogleAuth()

gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file in file_list:
  print(f"title: {file['title']}, id: {file['id']}")

答案 1 :(得分:0)

解决方案

您可以使用强大的PyDrive库从Google合作实验室轻松访问您的云端硬盘文件,从而检查您已导出或导出了哪些文件,等等。

以下代码是一个示例,列出了Google Drive API根目录中的所有文件。这是在this answer 中找到的(是的,我正在将此答案发布为社区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']))

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

我希望这对您有所帮助。让我知道您是否需要其他任何东西,或者您是否不了解。 :)