python 刪除系統中的文件(按時間,大小,擴展名)
按時間刪除文件
# importing the required modulesimport osimport shutilimport time# main functiondef main():# initializing the countdeleted_folders_count = 0deleted_files_count = 0# specify the pathpath = '/PATH_TO_DELETE'# specify the daysdays = 30# converting days to seconds# time.time() returns current time in secondsseconds = time.time() - (days * 24 * 60 * 60)# checking whether the file is present in path or notif os.path.exists(path):# iterating over each and every folder and file in the pathfor root_folder, folders, files in os.walk(path):# comparing the daysif seconds >= get_file_or_folder_age(root_folder):# removing the folderremove_folder(root_folder)deleted_folders_count += 1 # incrementing count# breaking after removing the root_folderbreakelse:# checking folder from the root_folderfor folder in folders:# folder pathfolder_path = os.path.join(root_folder, folder)# comparing with the daysif seconds >= get_file_or_folder_age(folder_path):# invoking the remove_folder functionremove_folder(folder_path)deleted_folders_count += 1 # incrementing count# checking the current directory filesfor file in files:# file pathfile_path = os.path.join(root_folder, file)# comparing the daysif seconds >= get_file_or_folder_age(file_path):# invoking the remove_file functionremove_file(file_path)deleted_files_count += 1 # incrementing countelse:# if the path is not a directory# comparing with the daysif seconds >= get_file_or_folder_age(path):# invoking the fileremove_file(path)deleted_files_count += 1 # incrementing countelse:# file/folder is not foundprint(f’'{path}' is not found’)deleted_files_count += 1 # incrementing countprint(f'Total folders deleted: {deleted_folders_count}')print(f'Total files deleted: {deleted_files_count}')def remove_folder(path):# removing the folderif not shutil.rmtree(path):# success messageprint(f'{path} is removed successfully')else:# failure messageprint(f'Unable to delete the {path}')def remove_file(path):# removing the fileif not os.remove(path):# success messageprint(f'{path} is removed successfully')else:# failure messageprint(f'Unable to delete the {path}')def get_file_or_folder_age(path):# getting ctime of the file/folder# time will be in secondsctime = os.stat(path).st_ctime# returning the timereturn ctimeif __name__ == ’__main__’:main()
需要在上面的代碼中調整以下兩個變量
days = 30 path = '/PATH_TO_DELETE'
按大小刪除文件
# importing the os moduleimport os# function that returns size of a filedef get_file_size(path):# getting file size in bytessize = os.path.getsize(path)# returning the size of the filereturn size# function to delete a filedef remove_file(path):# deleting the fileif not os.remove(path):# successprint(f'{path} is deleted successfully')else:# errorprint(f'Unable to delete the {path}')def main():# specify the pathpath = 'ENTER_PATH_HERE'# put max size of file in MBssize = 500# checking whether the path exists or notif os.path.exists(path):# converting size to bytessize = size * 1024 * 1024# traversing through the subfoldersfor root_folder, folders, files in os.walk(path):# iterating over the files listfor file in files:# getting file pathfile_path = os.path.join(root_folder, file)# checking the file sizeif get_file_size(file_path) >= size:# invoking the remove_file functionremove_file(file_path)else:# checking only if the path is fileif os.path.isfile(path):# path is not a dir# checking the file directlyif get_file_size(path) >= size:# invoking the remove_file functionremove_file(path)else:# path doesn’t existprint(f'{path} doesn’t exist')if __name__ == ’__main__’:main()
調整以下兩個變量。
path = 'ENTER_PATH_HERE' size = 500
按擴展名刪除文件
在某些情況下,您想按文件的擴展名類型刪除文件。假設.log文件。我們可以使用該os.path.splitext(path)方法找到文件的擴展名。它返回一個元組,其中包含文件的路徑和擴展名。
# importing os moduleimport os# main functiondef main(): # specify the path path = 'PATH_TO_LOOK_FOR' # specify the extension extension = '.log' # checking whether the path exist or not if os.path.exists(path):# check whether the path is directory or not if os.path.isdir(path): # iterating through the subfolders for root_folder, folders, files in os.walk(path):# checking of the filesfor file in files: # file path file_path = os.path.join(root_folder, file) # extracting the extension from the filename file_extension = os.path.splitext(file_path)[1] # checking the file_extension if extension == file_extension:# deleting the file if not os.remove(file_path): # success message print(f'{file_path} deleted successfully') else: # failure message print(f'Unable to delete the {file_path}')else: # path is not a directory print(f'{path} is not a directory') else:# path doen’t exist print(f'{path} doesn’t exist')if __name__ == ’__main__’: # invoking main function main()
不要忘記更新上面代碼中的path和extension變量,以滿足您的要求。
以上就是python 刪除系統中的文件的詳細內容,更多關于python 刪除文件的資料請關注好吧啦網其它相關文章!
相關文章: