简体   繁体   English

如何删除谷歌驱动器中具有特定扩展名的所有文件

[英]how to delete all files with a specific extension in google drive

I have about 200k files with.tif extension in my root folder of google drive that i need to delete.我的谷歌驱动器的根文件夹中有大约 20 万个扩展名为 .tif 的文件,我需要删除这些文件。

The python code i wrote only transfers / deletes the few files that we can see at an instance (we need to scroll down in the drive and let them 'load' to see more of them)我编写的 python 代码仅传输/删除了我们在实例中可以看到的少数文件(我们需要在驱动器中向下滚动并让它们“加载”以查看更多文件)

I am willing to delete all other files as well if there is a shortcut to do so.如果有快捷方式,我也愿意删除所有其他文件。

Cntl + A does not work either, it just selects a same few files that we can see in an instance. Cntl + A 也不起作用,它只是选择我们可以在实例中看到的几个相同的文件。

import shutil
import os

source = '/content/gdrive/My Drive'
dest1 = '/content/gdrive/My Drive/toDelete'


files = os.listdir(source)

for f in files:
    if (f.endswith(".tif")):
        shutil.move(f, dest1)

dir_name = "/content/gdrive/My Drive"
test = os.listdir(dir_name)

for item in test:
    if item.endswith(".tif"):
        os.remove(os.path.join(dir_name, item))

First you need to search for all the files that contain in the name and are in your root directory once you have those you can start deleting them.首先,您需要搜索名称中包含并位于根目录中的所有文件,一旦有了这些文件,您就可以开始删除它们了。

I recommend you test this without the delete first to make sure its listing the files your after I am not responsible for this deleting stuff:)我建议您先在不删除的情况下对其进行测试,以确保在我不负责删除这些内容之后,它会列出您的文件:)

page_token = None page_token = 无

while True:
    response = drive_service.files().list(q="name contains '.tif' and 'root' in parents",
                                          spaces='drive',
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
    for file in response.get('files', []):
        # Process change
        print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
        #drive_service.files().delete(fileId=file.get('id')).execute()
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break
  1. Use glob .使用glob
  2. Use pathlib for path manipulation.使用pathlib进行路径操作。
import pathlib
import shutil

source = pathlib.Path('/content/gdrive/My Drive')
dest1 = pathlib.Path('/content/gdrive/My Drive/toDelete')
dest1.mkdir(exist_ok=True)

for f in source.glob("*.tif"):
    shutil.move(f, dest1.joinpath(f.name))

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

相关问题 使用 PyDrive 按扩展名删除 google drive 文件 - Delete google drive files by extension with PyDrive 如何列出特定谷歌驱动器目录 Python 中的所有文件 - How to list all the files in a specific google drive directory Python 如何从谷歌驱动器中删除文件? - How to delete files from google drive? 如何在谷歌驱动器中搜索特定文件? - How to search for specific files in google drive? 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 请求使用谷歌驱动器 API 上传和删除文件 - How to upload and delete files with google drive API using python requests 如何使用 Google Drive API 一次删除多个文件 - How to delete multiple files at once using Google Drive API Python:如何使用 folderId 将文件上传到 Google Drive 中的特定文件夹 - Python: How to upload files to a specific folder in Google Drive using folderId 如何列出Google云端硬盘文件夹的所有文件,文件夹,子文件夹和子文件 - How to list all files, folders, subfolders and subfiles of a Google drive folder 谷歌驱动器 API:如何找到我与他人共享的所有文件 - Google drive API : How to find all files I shared with others
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM