Skip to content

Instantly share code, notes, and snippets.

@princeppy
Created August 12, 2023 08:30
Show Gist options
  • Save princeppy/64f4ca31b49e201b6225781a21075ec8 to your computer and use it in GitHub Desktop.
Save princeppy/64f4ca31b49e201b6225781a21075ec8 to your computer and use it in GitHub Desktop.
delete files which are modified on a particular date
import glob
import os
import time
dir_name = '/content/drive/MyDrive/'
target_date = datetime.datetime.strptime('2023-03-30', '%Y-%m-%d').date()
print(target_date)
# Get list of all files only in the given directory
list_of_files = filter( os.path.isfile, glob.glob(dir_name + '*') )
filtered_files = [
file
for file in list_of_files
if datetime.datetime.fromtimestamp(os.path.getmtime(file)).date() == target_date
]
# Sort list of files based on last modification time in ascending order
list_of_files = sorted( filtered_files, key = os.path.getmtime)
# Iterate over sorted list of files and print file path
# along with last modification time of file
for file_path in list_of_files:
timestamp_str = time.strftime( '%m/%d/%Y :: %H:%M:%S', time.gmtime(os.path.getmtime(file_path)))
print(timestamp_str, ' -->', file_path)
# os.remove(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment