简体   繁体   English

使用 Python 将 Google Drive 文件下载到特定位置

[英]Download Google Drive files to a specific location using Python

Hello all ,大家好
I would like to download google-drive files directly to a folder on my pc rather than the standard download folder.我想将 google-drive 文件直接下载到我电脑上的文件夹而不是标准下载文件夹。 Also, the name of the file should stay the same and not manually set.此外,文件的名称应保持不变,而不是手动设置。

I have tried downloading the files using the direct download link which downloads the file but you cannot decide where the file saves on your computer.我曾尝试使用下载文件的直接下载链接下载文件,但您无法决定文件在计算机上的保存位置。

I also have tried these methods:我也试过这些方法:
https://stackoverflow.com/a/39225272/13715296 (This method did not work for me) https://stackoverflow.com/a/47761459/13715296 (With this method I could not have the original name of the file) https://stackoverflow.com/a/39225272/13715296 (这个方法对我不起作用

In my code I basically have a lot of these type of urls:在我的代码中,我基本上有很多这些类型的网址:
https://drive.google.com/file/d/xxxxxxxxxxxxxxxxxxx/view?usp=drive_web https://drive.google.com/file/d/xxxxxxxxxxxxxxxxxxx/view?usp=drive_web

But I can easily convert them to these direct download urls:但我可以轻松地将它们转换为这些直接下载网址:
https://drive.google.com/u/0/uc?id=xxxxxxxxxxxxxxxxxxx&export=download https://drive.google.com/u/0/uc?id=xxxxxxxxxxxxxxxxxxx&export=download

I just have not found a method on how to download the files using python to a specific folder while keeping the original name of the file the same.我只是还没有找到一种方法,如何使用 python 将文件下载到特定文件夹,同时保持文件的原始名称相同。

SOLUTION解决方案

With the method suggested by @Jacques-GuzelHeron I have now this code:使用@Jacques-GuzelHeron 建议的方法,我现在有了以下代码:

    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('drive', 'v3', credentials=creds)
    for id_url in list_urls:
        file_id = id_url
        results = service.files().get(fileId=file_id).execute()
        name = results['name']
        request = service.files().get_media(fileId=file_id)
        fh = io.BytesIO()
        downloader = MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Download %d%%." % int(status.progress() * 100))

        fh.seek(0)
        # Write the received data to the file
        path = "PATH/TO/MY/FOLDER/" + name
        with open(path, 'wb') as f:
            shutil.copyfileobj(fh, f)

This is the code provided from the python quickstart page and the example code .这是python 快速入门页面示例代码提供的代码

I used the google-drive-api to search for the name by ID, which I could later add back into the path:我使用 google-drive-api 按 ID 搜索名称,稍后我可以将其添加回路径中:

        path = "PATH/TO/MY/FOLDER/" + name
        with open(path, 'wb') as f:
            shutil.copyfileobj(fh, f)

This allowed me to control the path where I stored the download and keep the name of the file unchanged.这使我可以控制存储下载的路径并保持文件名不变。 Definitely not my best code, but it did do the job.绝对不是我最好的代码,但它确实完成了工作。

I understand that you have an array of Drive file links, and you want to download them locally using Python.我了解到您有一系列 Drive 文件链接,并且您想使用 Python 在本地下载它们。 I am assuming that you want to to download files stored on Drive, not Workspace files (ie Docs, Sheets…).我假设您要下载存储在 Drive 上的文件,而不是 Workspace 文件(即文档、表格……)。 You can do it very easily by following the Drive API Python quickstart guide.您可以按照Drive API Python 快速入门指南轻松完成此操作。 That walkthrough will install all the necessary dependencies and show you an example code.该演练将安装所有必要的依赖项并向您展示示例代码。 Then, you only have to edit the main function to download the files instead of the sample operations.然后,您只需编辑主 function 即可下载文件,而不是示例操作。

To download the Python files you only need to know its id and use the Files.get method.要下载 Python 文件,您只需要知道它的 id 并使用Files.get方法。 I see that you already know the ids, so you are ready to make the request.我看到您已经知道 ID,因此您已准备好提出请求。 To build the request you should introduce the id of the file and set the parameter alt to the value media .要构建请求,您应该引入文件的 id 并将参数alt设置为值media If you are using the example from the paragraph above, you can do it just by using the id like this example .如果您使用的是上一段中的示例,则只需使用 id 即可,如 this example If those guides don't work for you, please let me know.如果这些指南对您不起作用,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 从 Google Drive 下载文件 - Download Files From Google Drive Using Python 如何使用 python 以编程方式从谷歌驱动器下载特定文件 - How can I download specific files from google drive programmatically using python Python:使用 url 从谷歌驱动器下载文件 - Python: download files from google drive using url 使用python将文件上传到特定的Google Drive文件夹 - Upload files to a specific Google Drive folder using python Python:如何使用 folderId 将文件上传到 Google Drive 中的特定文件夹 - Python: How to upload files to a specific folder in Google Drive using folderId Google drive api python - 如何将文件夹或文件夹中的所有文件下载到特定的本地目标路径 - Google drive api python - How to download all the files inside a folder or the folder to a specific local destination path 如何使用 Python 在 Google Drive 上下载目录? - How to Download a directory on Google Drive using Python? Python Google Drive API 下载私人文件 Google 登录屏幕 - Python Google Drive API Download Private Files Google Login Screen 谷歌驱动器API下载文件 - python - 没有下载文件 - Google drive API download files - python - no files downloaded 如何使用 Google colab 将文件下载到 Google Drive? - How to download files to Google Drive using Google colab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM