Python methods for deleting outdated files

Source: Internet
Author: User
Tags glob
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.