亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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 編程
相關文章:
主站蜘蛛池模板: 国产综合久久 | 精品国产欧美精品v | 成年美女黄网站色大片免费看 | 国产福利视频一区二区微拍视频 | xxⅹ丰满妇女与善交 | 国产在线拍揄自揄视频不卡99 | 国产麻豆一级在线观看 | 国产精品免费观看视频播放 | 婷婷丁香啪啪 | 国产成人啪午夜精品网站 | 中文字幕在线不卡 | 在线高清一级欧美精品 | 国产精品嫩草影院在线看 | 国产黄拍| 欧美成综合网网站 | 欧美一级视频精品观看 | 亚洲免费片 | 国产成人精品曰本亚洲78 | 在线观看国产精品一区 | 制服丝袜中文字幕在线观看 | 国产一区二区成人 | 亚洲精选在线观看 | 欧美xxxx69tube8护士 | 日韩高清特级特黄毛片 | 久久久久激情免费观看 | 国产精品你懂的在线播放调教 | 欧美日韩国产高清一区二区三区 | 黄网免费观看 | 久久草草 | 久久538 | 亚洲夜色综合久久 | 麻豆精品在线视频 | 青娱乐91在线 | 亚洲视频在线观看免费 | 中文字幕在线不卡精品视频99 | 久久九九免费 | 网址大全在线免费观看 | 在线亚洲精品防屏蔽 | 日韩伦理一区二区三区 | 成人性生免费视频 | 欧美专区一区 |