国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

瀏覽:3日期: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 編程
相關文章:
主站蜘蛛池模板: 尹人在线视频 | 久久久久亚洲精品影视 | 免费高清一级欧美片在线观看 | 男人的天堂网在线 | 色婷婷久久综合中文久久蜜桃 | 国产丝袜不卡一区二区 | 亚洲影院国产 | 欧美一级鲁丝片 | 动漫精品一区二区 | 亚洲欧美成人综合在线 | 国产或人精品日本亚洲77美色 | 久色国产| 91久久精品一区二区 | 国产成人看片免费视频观看 | 偷自拍第一页 | 久久九九国产精品怡红院 | 在线观看视频国产 | 亚洲人成免费网站 | 亚洲在线不卡 | 精品自拍视频在线观看 | 在线免费成人 | 精品自拍一区 | a级片在线观看 | 老司机一级片 | 国产日本在线 | 成人的天堂 | 成人免费一区二区三区视频软件 | 久久久久久一品道精品免费看 | 欧美二区视频 | 亚洲网在线观看 | 久久天天躁综合夜夜黑人鲁色 | 波多野结衣在线视频免费观看 | 大学生一级一片第一次欧美 | 欧美特级毛片aaaa | 国产99久久亚洲综合精品 | 亚洲福利国产精品17p | 欧美一区二区三区免费不卡 | 亚洲成人综合视频 | 天天视频一区二区三区 | 亚洲国产高清视频 | 久久久久久久久一级毛片 |