Delete a Python script with the *.rar suffix in a specific directory on Windows

Source: Internet
Author: User
Tags python script

Import os,fnmatch,datetime,timedef all_files (root,pattern= ' * ', single_level=false,yield_folders=false):    Patterns = Pattern.split (';')    For path,subirs,files in Os.walk (root):        if yield_folders:            files.extend (subirs)        files.sort ()        for Name in Files: for            pattern in patterns:                if Fnmatch.fnmatch (name,pattern):                    yield Os.path.join (path,name)                    break        if single_level:            breakfor path in All_files ("e:/", ' *.rar '):    print (path)    Create_file _time = os.path.getatime (path) #文件创建的时间戳    ltime = Time.localtime (create_file_time)    timestr = Time.strftime (" %y-%m-%d%h:%m:%s ", ltime)  #验证时间戳转换为年月日格式, test with    print (timestr)    age_in_days = ((Time.time ()-create_ File_time)/(60*60*24))    if age_in_days >:        os.remove (path)        print (' Remove file:%s '% path)    else:        print ("File creation days less than 20 days")

  

The purpose of the script is to delete the RAR files with a date of 20 days before the D drive, which are under different directories.

Reference: http://www.iplaypy.com/sys/s104.html

Delete a Python script with the *.rar suffix in a specific directory on Windows

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.