Skip to content

Instantly share code, notes, and snippets.

Forked from xflr6/
Created December 6, 2020 00:22
Show Gist options
  • Save obonyojimmy/f200c747682ef87f350428d5a74b8047 to your computer and use it in GitHub Desktop.
Save obonyojimmy/f200c747682ef87f350428d5a74b8047 to your computer and use it in GitHub Desktop.
Recursively traverse the directory tree of a Google Drive folder as variation of os.walk()
# - os.walk variation with Google Drive API
import os
from apiclient.discovery import build # pip install google-api-python-client
FOLDER = 'application/'
def get_credentials(scopes, secrets='~/client_secrets.json', storage='~/storage.json'):
from oauth2client import file, client, tools
store = file.Storage(os.path.expanduser(storage))
creds = store.get()
if creds is None or creds.invalid:
flow = client.flow_from_clientsecrets(os.path.expanduser(secrets), scopes)
flags = tools.argparser.parse_args([])
creds = tools.run_flow(flow, store, flags)
return creds
creds = get_credentials(scopes=[''])
service = build('drive', version='v3', credentials=creds)
def iterfiles(name=None, is_folder=None, parent=None, order_by='folder,name,createdTime'):
q = []
if name is not None:
q.append("name = '%s'" % name.replace("'", "\\'"))
if is_folder is not None:
q.append("mimeType %s '%s'" % ('=' if is_folder else '!=', FOLDER))
if parent is not None:
q.append("'%s' in parents" % parent.replace("'", "\\'"))
params = {'pageToken': None, 'orderBy': order_by}
if q:
params['q'] = ' and '.join(q)
while True:
response = service.files().list(**params).execute()
for f in response['files']:
yield f
params['pageToken'] = response['nextPageToken']
except KeyError:
def walk(top='root', by_name=False):
if by_name:
top, = iterfiles(name=top, is_folder=True)
top = service.files().get(fileId=top).execute()
if top['mimeType'] != FOLDER:
raise ValueError('not a folder: %r' % top)
stack = [((top['name'],), top)]
while stack:
path, top = stack.pop()
dirs, files = is_file = [], []
for f in iterfiles(parent=top['id']):
is_file[f['mimeType'] != FOLDER].append(f)
yield path, top, dirs, files
if dirs:
stack.extend((path + (d['name'],), d) for d in reversed(dirs))
for kwargs in [{'top': 'spam', 'by_name': True}, {}]:
for path, root, dirs, files in walk(**kwargs):
print('%s\t%d %d' % ('/'.join(path), len(dirs), len(files)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment