1

I am using pyDrive to get a list of all files in specific folder id's. If I hard code the folder id, it works fine, but I'd like to use a loop, and cycle through a list of folder ID's. It might just be that I'm not formatting the variable into the command correctly.

This works fine....

file_list = drive.ListFile({'q': "'0B1fhQb9wymxEUUFGVXpfYlJhTk0' in parents and trashed=false"}).GetList()

But if I put that statement into a for loop to cycle through a list of folder ID's, it does not work. The '0B1fhQb9wymxEUUFGVXpfYlJhTk0' is the part I need to swap out for the variable in the for loop.

I've tried every kind of for loop I can find an example of on the net, and every kind of way to substitute the loop variable. Even tried putting the first half of the command in a string, the last half in another, and then "first + x + last", but that didn't work either.

for x in listofpersonfolders:
  file_list = drive.ListFile({'q': "\'x\' in parents and trashed=false"}).GetList()
  print file_list

and also tried

for x in listofpersonfolders:
  file_list = drive.ListFile({'q': "'x' in parents and trashed=false"}).GetList()
  print file_list
CDspace
  • 2,639
  • 18
  • 30
  • 36
Aegis
  • 13
  • 4

2 Answers2

1

Try this:

# Define string with placeholder for parent id.
request_template = "'{parent_id}' in parents and trashed=false"

for x in listofpersonfolders:
  # Replace the placeholder for parent_id with x in each iteration.
  file_list = drive.ListFile({'q': request_template.format(parent_id=x)}).GetList()
  print file_list

You can check out this website for more information on string formatting: https://pyformat.info/

Robin Nabel
  • 2,170
  • 1
  • 21
  • 26
  • Thank you for taking a moment to help. I tried that solution. Have tried a bunch of similar lines. This is what I got with this one.....sigh...my cut and paste was 800+ char too long, it returned "Invalid query" Thank you for pointing me to pyformat.info. Will check that out. – Aegis Feb 05 '17 at 15:48
  • No problem! I just double-checked the query and I think found the error: the folder ID has to be surrounded by quotes. This is now changed - maybe try again? – Robin Nabel Feb 06 '17 at 16:09
0

This is a solution

for x in listofpersonfolders:
    file_list = drive.ListFile({'q': "'{}' in parents and trashed=false".format(x)}).GetList()
print file_list
Rafael
  • 433
  • 6
  • 12