Riko Hamblin
Riko Hamblin

Reputation: 61

Google Drive API with python not getting most current directories

I am walking through the files on my google drive however if I run my code, delete a folder and run my code again the deleted folder is still being read somehow. Even when I add new folders to my google drive the new + deleted one will be read. Any help is appreciated

creds = store.get()

if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('systemFiles/client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))

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
        try:
            params['pageToken'] = response['nextPageToken']
        except KeyError:
            return

def walk(top):
    top, = iterfiles(name=top, is_folder=True)
    stack = [((top['name'],), [top])]
    while stack:
        path, tops = stack.pop()
        for top in tops:
            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.append((path + (top['name'],), dirs))

if __name__ == "__main__":

    try:
        os.remove('CUSTOM IMAGES')
    except:
        pass

    for path, root, dirs, files in walk('Folder X'):
    if (len(dirs) == 0):
        saveTo = str(path[len(path) - 2] + '/' + path[len(path) - 1]).rstrip().lstrip()
        textFileName = str(root['name']).rstrip().lstrip() + '.txt'
        print 'Saving To:', saveTo + '\t --->> \t' + 'Filename:', textFileName

Upvotes: 0

Views: 318

Answers (1)

Tanaike
Tanaike

Reputation: 201378

When it uses q of service.files().list, at the default value of q, all files with trashed: true and trashed: false are retrieved.

So if you don't want to retrieve the files in the trash box, please add trashed = false to q.

Note :

  • If you use trashed = true to q, only files in the trash box are retrieved.

Reference :

Upvotes: 1

Related Questions