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

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

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

瀏覽:4日期: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 編程
相關文章:
主站蜘蛛池模板: 欧美九九视频 | 高清国产在线播放成人 | 国产成综合 | 成人免费公开视频 | 日本乱理伦中文三区 | 欧美一级片 在线播放 | 激情五月色播五月 | 国产片网址 | 国产永久在线视频 | 成人性版蝴蝶影院污 | 中文字幕一区在线观看 | 亚州不卡| 国产最猛性xxxxxx69交 | 天天做天天爱夜夜大爽完整 | 欧美三级真做在线观看 | 国产一区二区三区在线观看精品 | 久久精品国产大片免费观看 | 久久99精品免费视频 | 国产黄色三级三级三级 | 亚洲一区二区精品视频 | 香蕉视频黄色在线观看 | 2017天天爽夜夜爽精品视频 | 国内精品一区二区在线观看 | 殴美一级视频 | 日韩特黄特色大片免费视频 | 精品国产一区二区三区四区不 | 三级理论手机在线观看视频 | 五月色婷婷综合开心网亚 | 国产男女猛烈无遮档免费视频网站 | 国产精品一级毛片不收费 | 中文字幕亚洲欧美 | 免费播放欧美毛片欧美aaaaa | 毛片无码国产 | 欧美白人和黑人xxxx猛交视频 | 国内国语一级毛片在线视频 | 欧美一级精品高清在线观看 | 精品在线小视频 | 99精品视频在线观看免费播放 | 男人的天堂欧美 | 91久久国产露脸精品免费 | 国产一区二区三区亚洲欧美 |