The example in this article describes how Python deletes outdated files. Share to everyone for your reference. The implementation method is as follows:
# Remove all JPEG image files of a expired modification date = mtime# You could also use creation date (CTime) or Last AC Cess Date (atime) # os.stat (filename) returns (mode, Ino, Dev, nlink, uid, gid, size, atime, Mtime, CTime) # Tested with Pyt HON24 vegaseat 6/7/2005import os, glob, timeroot = ' d:\\vacation\\poland2003\\ ' # one specific folder#root = ' D:\\Vacatio n\\* ' # or all the subfolders too# expiration date in the format yyyy-mm-ddxdate = ' 2003-12-31 ' print '-' *50for folder In Glob.glob (root): Print folder # here. jpg image files, but could be. txt files or whatever for image in Glob.glob (fo Lder + '/*.jpg '): # Retrieves the stats for the current JPEG image file # The tuple element at index 8 is the LAST-M Odified-date stats = os.stat (image) # put the dates into matching format lastmoddate = time.localtime (stats [8]) Expdate = Time.strptime (xdate, '%y-%m-%d ') print image, Time.strftime ("%m/%d/%y", lastmoddate) # Check if im Age-last-modified-date Is outdated if expdate > lastmodDate:try:print ' removing ', image, Time.strftime ("(older than%m/%d/%y)" , expdate) #os. Remove (image) # commented out for testing except Oserror:print ' Could not remove ', imag E
Hopefully this article will help you with Python programming.