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

时间:2019-10-02 06:46:19

标签: python google-drive-api

我需要删除的Google驱动器根文件夹中有200k带有.tif扩展名的文件。

我编写的python代码仅传输/删除了我们在实例中可以看到的几个文件(我们需要在驱动器中向下滚动并让它们“加载”以查看更多文件)

如果有快捷方式,我也愿意删除所有其他文件。

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))

2 个答案:

答案 0 :(得分:1)

首先,您需要搜索名称中包含的所有文件以及位于根目录中的所有文件,您可以开始删除它们。

我建议您先进行测试,然后再删除,以确保在我对删除的内容不承担任何责任后,将其列出您的文件:)

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 :(得分:0)

  1. 使用SELECT * FROM `wp_postmeta` where meta_key = "_wp_attached_file"
  2. 使用select NODATA_records, main.hotel_id, main.site_id, main.eapid,main.tpid, main.client_id, main.message_type, dense_rank() over (order by NODATA_records desc) AS Causerank FROM ( SELECT SUM(a.No_data_records) AS NODATA_records, b.hotel_id, b.site_id, b.eapid, b.tpid, b.client_id, b.message_type FROM ( SELECT COUNT(*) AS No_data_records, shopping_request_uid FROM dm.shopping_package WHERE shopped_datetime::date = current_date-2 GROUP BY shopping_request_uid )a INNER JOIN ( SELECT count(*) AS No_data_records, shopping_request_uid, hotel_id, site_id, eapid, tpid, client_id, message_type FROM dm.shopping_product WHERE product_type='H' AND shopped_datetime::date =current_date-2 GROUP BY shopping_request_uid,hotel_id,site_id,eapid,tpid,client_id,message_type )b ON a.shopping_request_uid=b.shopping_request_uid GROUP BY b.hotel_id,b.site_id,b.eapid,b.tpid,b.client_id,b.message_type ) main 进行路径操作。
glob