Python delete files from 30 days ago

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.