User directory due to perennial users upload pictures more, resulting in hard disk resources will be exhausted, but customers require at least one months to save files,
However, the folder of hundreds of thousands of pictures, whether open, sort delete is very consuming server performance, because loading these 10 g files will inevitably cause memory and CPU
A lot of consumption, so write a Python script to automatically delete files from 30 days ago
The code is as follows
#-*-coding:utf-8-*-ImportOSImport TimeImportDATETIMEF= List (Os.listdir ('G:\\QTP')) forIinchRange (len (f)): Filedate= Os.path.getmtime ('g:\\qtp\\'+F[i]) time1= Datetime.datetime.fromtimestamp (filedate). Strftime ('%y-%m-%d') Date1=time.time () num1= (date1-filedate)/60/60/24ifNUM1 >= 30: Os.remove ('g:\\qtp\\'+F[i])Print("deleted file:%s:%s"%(time1, F[i]))Else: Print("There is no file more than")
:
end!
Python delete files from 30 days ago