Reputation: 161
I have about 200k files with .tif extension in my root folder of google drive that i need to delete.
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)
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.
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))
Upvotes: 2
Views: 2672
Reputation: 116869
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
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
Upvotes: 2
Reputation: 815
glob
.pathlib
for path manipulation.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))
Upvotes: 0