使用PyDrive(Python)访问文件夹,子文件夹和子文件 [英] Accessing folders, subfolders and subfiles using PyDrive (Python)

查看:220
本文介绍了使用PyDrive(Python)访问文件夹,子文件夹和子文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从PyDrive文档中获得了以下代码,该代码允许访问我的Google云端硬盘中的顶级文件夹.我想从中访问所有文件夹,子文件夹和文件.我将如何去做(我刚刚开始使用PyDrive)?

I have the following code from the PyDrive documentation that allows access to top level folders in my Google Drive. I would like to access all of the folders, subfolders, and files from it. How would I go about doing this (I just started using PyDrive)?

#!/usr/bin/python
# -*- coding: utf-8 -*-
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive


gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication

#Make GoogleDrive instance with Authenticated GoogleAuth instance
drive = GoogleDrive(gauth)

#Google_Drive_Tree = 
# Auto-iterate through all files that matches this query
top_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file in top_list:
    print 'title: %s, id: %s' % (file['title'], file['id'])
    print "---------------------------------------------"

#Paginate file lists by specifying number of max results
for file_list in drive.ListFile({'q': 'trashed=true', 'maxResults': 10}):
    print 'Received %s files from Files.list()' % len(file_list) # <= 10
    for file1 in file_list:
        print 'title: %s, id: %s' % (file1['title'], file1['id'])

我已经检查了以下页面

I have checked the following page How to list all files, folders, subfolders and subfiles of a Google drive folder , which seemed to be the answer I was looking for but the code is not there anymore.

推荐答案

您的代码绝对正确.但是,使用Pydrive的默认设置,您只能访问根级别的文件和文件夹. 在settings.yaml文件中更改oauth_scope可以解决此问题.

Your code is absolutely correct. But with the default settings of Pydrive , you have access to only the root level files and folders. Changing oauth_scope in settings.yaml file fixes this issue.

client_config_backend: settings
client_config:
client_id: XXX
client_secret: XXXX

save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json

get_refresh_token: True

oauth_scope:
  - https://www.googleapis.com/auth/drive
  - https://www.googleapis.com/auth/drive.metadata

这篇关于使用PyDrive(Python)访问文件夹,子文件夹和子文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆