av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

python 刪除系統中的文件(按時間,大小,擴展名)

瀏覽:2日期:2022-07-04 18:17:22

按時間刪除文件

# 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 刪除文件的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 一区二区三区不卡视频 | 亚洲 欧美 另类 综合 偷拍 | 久久久久久99 | 91精品国产综合久久小仙女图片 | 亚洲三级在线观看 | 蜜臀网| 久久亚洲国产精品日日av夜夜 | 一区二区三区四区电影 | 成人免费在线视频 | ririsao久久精品一区 | 日韩欧美三级 | 视频一区中文字幕 | 亚洲精品黄色 | 韩国毛片一区二区三区 | 国产婷婷| 亚洲一级黄色 | 色精品| 久久国产秒| 亚洲毛片在线观看 | 精品一区av | 久久久国产一区二区三区 | 亚洲视频一区二区三区 | 中文字幕综合在线 | 成人在线中文字幕 | 不卡一区二区三区四区 | 久久成人免费视频 | 亚洲一区二区在线免费观看 | 91精品国产91久久久久久吃药 | 99精品在线| 亚洲一区二区在线免费观看 | 中文字幕在线网 | 国产精品无码专区在线观看 | 黄色片av | 精品在线免费观看视频 | 涩涩视频在线观看 | 天天爽网站 | 99国产精品99久久久久久粉嫩 | 久久男人 | 风间由美一区二区三区在线观看 | 亚洲精品欧美一区二区三区 | 欧美一区二区综合 |