[英]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
glob
.glob
。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.