1

我正在使用 pyDrive 获取特定文件夹 ID 中所有文件的列表。如果我对文件夹 ID 进行硬编码,它可以正常工作,但我想使用循环,并循环浏览文件夹 ID 的列表。可能只是我没有将变量正确格式化为命令。

这工作正常....

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

但是,如果我将该语句放入 for 循环中以循环浏览文件夹 ID 列表,则它不起作用。'0B1fhQb9wymxEUUFGVXpfYlJhTk0' 是我需要在 for 循环中换出变量的部分。

我已经尝试了各种我可以在网上找到的例子的 for 循环,以及各种替换循环变量的方法。甚至尝试将命令的前半部分放在一个字符串中,后半部分放在另一个字符串中,然后是“first + x + last”,但这也不起作用。

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

也试过

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

2 回答 2

1

尝试这个:

# 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

您可以查看此网站以获取有关字符串格式的更多信息:https ://pyformat.info/

于 2017-02-04T11:10:18.827 回答
0

这是一个解决方案

for x in listofpersonfolders:
    file_list = drive.ListFile({'q': "'{}' in parents and trashed=false".format(x)}).GetList()
print file_list
于 2017-02-15T19:09:03.623 回答