fwarren icon

Delete Obsolete Files

fwarren | PRO | 03/14/17 06:39:14 PM UTC | 0 ⭐ | 296 👁️ | Never ⏰ | []
Python |

820 B

|

None

|

0 👍

/

0 👎

import os, glob
import datetime as dt
 
def makeday(text):
  return dt.datetime.strptime(text, "%Y-%m-%d").date()
 
def delete_obsolete_slides(filespec, day)
    """delete old files
 
    Args:
        filespec (string): directory path and filespec, ex: /tmp/*.pdf
        day (datetime.date): any files older than this date match
    """
 
    for fn in glob.glob(filespec):
        stamp = dt.datetime.fromtimestamp(os.path.getmtime(fn)).date()
        if stamp < day:
            os.remove(fn)
 
""" I love the langeage
From the datetime library using the datetime object can call the fromtimestamp function 
to convert a timestamp into a date-time value and then truncate that date-time value to a date value
"""
 
# Example Run:
threshhold = makeday('2017-02-15')
delete_obsolete_slides('/tmp/*.png", threshhold)

Comments