Python deletes a file with a. Log prefix for 2 days

Source: Internet
Author: User
Tags python script

Python script deletes files with a. Log prefix for 2 days

#!/usr/local/python/bin/python#-*-coding=utf8-*-import Timeimport os,sysn = 2 #设置删除多少天前的文件def deletefile (path):        For Eachfile in Os.listdir (path):                filename = os.path.join (path,eachfile)                if Os.path.isfile (filename):                            LastModifyTime = Os.stat (filename). st_mtime                            endfiletime = time.time ()-3600 * * N #设置删除多久之前的文件                            if Endfiletime > lastmodifytime:                                if filename[-4:]== ". Log":                                    os.remove (filename)                                    #print "Delete file%s succeeded"% filename                Elif os.path.isdir (filename): #如果是目录则递归调用当前函数                        deletefile (filename) if __name__ = = ' __main__ ':        Path = R '/home /logs/tomcat/'        deletefile (path)

Set up crontab tasks

Python deletes a file with a. Log prefix for 2 days

Related Article

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.