python 删除系统中的文件(按时间,大小,扩展名)
按时间删除文件
#importingtherequiredmodules importos importshutil importtime #mainfunction defmain(): #initializingthecount deleted_folders_count=0 deleted_files_count=0 #specifythepath path="/PATH_TO_DELETE" #specifythedays days=30 #convertingdaystoseconds #time.time()returnscurrenttimeinseconds seconds=time.time()-(days*24*60*60) #checkingwhetherthefileispresentinpathornot ifos.path.exists(path): #iteratingovereachandeveryfolderandfileinthepath forroot_folder,folders,filesinos.walk(path): #comparingthedays ifseconds>=get_file_or_folder_age(root_folder): #removingthefolder remove_folder(root_folder) deleted_folders_count+=1#incrementingcount #breakingafterremovingtheroot_folder break else: #checkingfolderfromtheroot_folder forfolderinfolders: #folderpath folder_path=os.path.join(root_folder,folder) #comparingwiththedays ifseconds>=get_file_or_folder_age(folder_path): #invokingtheremove_folderfunction remove_folder(folder_path) deleted_folders_count+=1#incrementingcount #checkingthecurrentdirectoryfiles forfileinfiles: #filepath file_path=os.path.join(root_folder,file) #comparingthedays ifseconds>=get_file_or_folder_age(file_path): #invokingtheremove_filefunction remove_file(file_path) deleted_files_count+=1#incrementingcount else: #ifthepathisnotadirectory #comparingwiththedays ifseconds>=get_file_or_folder_age(path): #invokingthefile remove_file(path) deleted_files_count+=1#incrementingcount else: #file/folderisnotfound print(f'"{path}"isnotfound') deleted_files_count+=1#incrementingcount print(f"Totalfoldersdeleted:{deleted_folders_count}") print(f"Totalfilesdeleted:{deleted_files_count}") defremove_folder(path): #removingthefolder ifnotshutil.rmtree(path): #successmessage print(f"{path}isremovedsuccessfully") else: #failuremessage print(f"Unabletodeletethe{path}") defremove_file(path): #removingthefile ifnotos.remove(path): #successmessage print(f"{path}isremovedsuccessfully") else: #failuremessage print(f"Unabletodeletethe{path}") defget_file_or_folder_age(path): #gettingctimeofthefile/folder #timewillbeinseconds ctime=os.stat(path).st_ctime #returningthetime returnctime if__name__=='__main__': main()
需要在上面的代码中调整以下两个变量
days=30 path="/PATH_TO_DELETE"
按大小删除文件
#importingtheosmodule importos #functionthatreturnssizeofafile defget_file_size(path): #gettingfilesizeinbytes size=os.path.getsize(path) #returningthesizeofthefile returnsize #functiontodeleteafile defremove_file(path): #deletingthefile ifnotos.remove(path): #success print(f"{path}isdeletedsuccessfully") else: #error print(f"Unabletodeletethe{path}") defmain(): #specifythepath path="ENTER_PATH_HERE" #putmaxsizeoffileinMBs size=500 #checkingwhetherthepathexistsornot ifos.path.exists(path): #convertingsizetobytes size=size*1024*1024 #traversingthroughthesubfolders forroot_folder,folders,filesinos.walk(path): #iteratingoverthefileslist forfileinfiles: #gettingfilepath file_path=os.path.join(root_folder,file) #checkingthefilesize ifget_file_size(file_path)>=size: #invokingtheremove_filefunction remove_file(file_path) else: #checkingonlyifthepathisfile ifos.path.isfile(path): #pathisnotadir #checkingthefiledirectly ifget_file_size(path)>=size: #invokingtheremove_filefunction remove_file(path) else: #pathdoesn'texist print(f"{path}doesn'texist") if__name__=='__main__': main()
调整以下两个变量。
path="ENTER_PATH_HERE" size=500
按扩展名删除文件
在某些情况下,您想按文件的扩展名类型删除文件。假设.log文件。我们可以使用该os.path.splitext(path)方法找到文件的扩展名。它返回一个元组,其中包含文件的路径和扩展名。
#importingosmodule importos #mainfunction defmain(): #specifythepath path="PATH_TO_LOOK_FOR" #specifytheextension extension=".log" #checkingwhetherthepathexistornot ifos.path.exists(path): #checkwhetherthepathisdirectoryornot ifos.path.isdir(path): #iteratingthroughthesubfolders forroot_folder,folders,filesinos.walk(path): #checkingofthefiles forfileinfiles: #filepath file_path=os.path.join(root_folder,file) #extractingtheextensionfromthefilename file_extension=os.path.splitext(file_path)[1] #checkingthefile_extension ifextension==file_extension: #deletingthefile ifnotos.remove(file_path): #successmessage print(f"{file_path}deletedsuccessfully") else: #failuremessage print(f"Unabletodeletethe{file_path}") else: #pathisnotadirectory print(f"{path}isnotadirectory") else: #pathdoen'texist print(f"{path}doesn'texist") if__name__=='__main__': #invokingmainfunction main()
不要忘记更新上面代码中的path和extension变量,以满足您的要求。
以上就是python删除系统中的文件的详细内容,更多关于python删除文件的资料请关注毛票票其它相关文章!