Delete Files Older Than 120 Days in a Directory
Tue Jul 23 2024 15:36:40 GMT+0000 (Coordinated Universal Time)
Saved by
@knguyencookie
#python
from datetime import datetime as dt , timedelta
print(len(os.listdir(processed_tm_sftp_files_root_dir)))
for f_dir in os.listdir(processed_tm_sftp_files_root_dir):
folder_path = f"{processed_tm_sftp_files_root_dir}\{f_dir}"
folder_date = f_dir.replace('texans_texans_texans_','').split('_')[0][:8]
folder_date = dt.strptime(folder_date,'%Y%m%d')
if folder_date < dt.today() - timedelta(days=120):
for f in os.listdir(folder_path):
file_path = f"{folder_path}\{f}"
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Deleted file: {file_path}")
content_copyCOPY
Comments