Delete old folders with date and time function

I am trying to delete old folders, and I ask, does anyone know how to set up a variable that allows me to check the variable 'todaystr', which today is equal to the date and minus 7 days of this line and saves it in another variable. I want to automatically delete old files in a week. The setting of the 'todaystr' variable is shown below.

todaystr = datetime.date.today().isoformat()  

I would like to create a variable "oldfile" that stores the current date minus 7 days so that I can delete a file with that date. Thanks for any help.

+3
source share
2 answers
import datetime
import os
import shutil

threshold = datetime.datetime.now() + datetime.timedelta(days=-7)
file_time = datetime.datetime.fromtimestamp(os.path.getmtime('/folder_name'))

if file_time < threshold:
    shutil.rmtree('/folder_name')
+4
source

, , , . , , , 7 . :

import datetime  
import os  
import calendar  

today = datetime.date.today()
todaystr = datetime.date.today().isoformat()
minus_seven = today.replace(day=today.day-7).isoformat()


if os.path.exists(minus_seven):
    os.system("sudo rm -rf "+minus_seven)
    print 'Sandboxes from 7 days ago removed'

linux , linux, , , .

0

Source: https://habr.com/ru/post/1726468/


All Articles