Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将变量传递给google drive api files()中的查询。list函数_Python_Google Drive Api - Fatal编程技术网

Python 将变量传递给google drive api files()中的查询。list函数

Python 将变量传递给google drive api files()中的查询。list函数,python,google-drive-api,Python,Google Drive Api,我试图创建一个函数,该函数接受目录名作为参数,并返回目录的ID。最终目标是使用返回的文件ID列出所述目录中的文件,但是我似乎无法理解将目录名作为变量传入的语法 我只能设法执行以下操作,其中我需要显式地键入目录的名称。请注意,我使用的是google drive api的v3 def get_folder_id(): folder_ID = "" while True: response = drive_service.files().list(

我试图创建一个函数,该函数接受目录名作为参数,并返回目录的ID。最终目标是使用返回的文件ID列出所述目录中的文件,但是我似乎无法理解将目录名作为变量传入的语法

我只能设法执行以下操作,其中我需要显式地键入目录的名称。请注意,我使用的是google drive api的v3

def get_folder_id():

    folder_ID = ""

    while True:
        response = drive_service.files().list(
            q="name='folder1'", fields="nextPageToken, files(id,name)").execute()

        files = response.get('files', [])

        for item in files:
            print ("Found file: %s, %s" % (item['name'], item['id']))
            folder_ID = item['id']

        page_token = response.get('nextPageToken', None)

        if page_token == None:
            break

    return folder_ID
如何将文件夹名作为参数传递,并仍将其包含在q查询中

我尝试过以下变体:

response = drive_service.files().list(
            q='"name="' + "'" + f_name + "'" + '"', fields="nextPageToken, files(id,name)").execute()
试图在请求周围包含单引号,但这也不起作用
  • 您希望使用带Python的驱动器API v3从文件夹名称中检索文件夹ID
  • 如果我的理解是正确的,那么使用以下搜索查询如何?请修改如下

    发件人: 致:
    • 在此修改中,已修改搜索查询。
      • mimeType='application/vnd.google apps.folder'
        表示文件夹
      • trashed=false
        表示垃圾箱之外的文件
    通过上述搜索查询,将检索文件夹名为
    f_name
    的文件夹

    参考:

    如果我误解了你的问题,而这不是你想要的结果,我道歉。

    不确定你是否已经得到了问题的答案。 但是,如果您想在GoogleDrive请求中将变量名作为查询的一部分传递,则在变量名前后加上+号。 年份_id=“诸如此类” 示例:-q=“父母在“+年”\u id+”中”


    快乐编码

    这并不是问题的关键,不过你已经回答了问题,谢谢。我面临的问题是,我无法获得正确的引号组合,以便将名称作为变量传入。谢谢大家!@欢迎光临。谢谢你让我知道。我很高兴你的问题解决了。如果您的问题已解决,请按“接受”按钮。与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果你找不到按钮,尽管告诉我@我很抱歉给您带来不便。您可以在上看到如何接受您的答案。将代码包装为“`````以使代码更易于阅读。我认为您在中也写了一个带有引号的错误
    q='"name="' + "'" + f_name + "'" + '"'
    
    q="name='" + f_name + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"