In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you how to use python to automatically clean up old files in folders, with detailed contents and clear logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Because the program keeps saving pictures all the time, you need to monitor the size of the picture folder and delete some of the earliest images once the specified size is exceeded.
In the way of opening the thread, the monitoring process is performed by keys at regular intervals in the thread.
Test folder size-> if it exceeds, sort the files in the folder according to the last modification time-> delete some of the earliest pictures-> monitor whether the folder size meets the requirements during deletion.
#-*-coding: utf-8-*-# # Open thread to detect the folder size, exceeding the specified size Sort by the last modification time of the file and delete some of the old pictures # import osimport threadingimport time # the file is sorted by the last modification time def get_file_list (file_path): dir_list = os.listdir (file_path) if not dir_list: return else: dir_list = sorted (dir_list, key=lambda x: os.path.getmtime (os.path.join (file_path) X)) # print (dir_list) return dir_list # get the folder size def get_size (file_path): totalsize=0 for filename in os.listdir (file_path): totalsize=totalsize+os.path.getsize (os.path.join (file_path) Filename) # print (totalsize / 1024 / 1024) return totalsize / 1024 / 1024 # 1 File Directory 2 maximum size of folder (M) 3 size to be deleted after (M) def detect_file_size (file_path, size_Max Size_Del): print (get_size (file_path)) if get_size (file_path) > size_Max: fileList = get_file_list (file_path) for i in range (len (fileList)): if get_size (file_path) > (size_Max-size_Del): print ("del:% d% s"% (I + 1) FileList [I]) os.remove (file_path + fileList [I]) # detect thread Def detectPicSize () is detected every 5 seconds: while True: print ('= detect=') detect_file_size (".. / pic/", 30 5) time.sleep (5) if _ _ name__ = "_ _ main__": # create a detection thread detect_thread = threading.Thread (target = detectPicSize) detect_thread.start () these are all the contents of the article "how to use python to automatically clean up old files in folders" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.