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