pythonbits

Untitled

Jan 18th, 2023
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | Source Code | 0 0
  1. # created by python bits. Check out my channel: https://www.youtube.com/@python-bits
  2.  
  3. import os
  4. import datetime
  5.  
  6. days = 15 #delete files older than given days
  7. folder = './' #current working directory
  8. current_time = datetime.datetime.now()
  9.  
  10.  
  11. for subdirs, dirs, files in os.walk(folder):
  12.     for file in files:
  13.         file_path = os.path.join(subdirs, file)
  14.         if os.path.isfile(file_path):
  15.             file_created_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
  16.             if current_time - file_created_time > datetime.timedelta(days=days):
  17.                 os.remove(file_path)
Advertisement
Add Comment
Please, Sign In to add comment