fwarren

Delete Obsolete Files

Mar 14th, 2017
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. import os, glob
  2. import datetime as dt
  3.  
  4. def makeday(text):
  5.   return dt.datetime.strptime(text, "%Y-%m-%d").date()
  6.  
  7. def delete_obsolete_slides(filespec, day)
  8.     """delete old files
  9.  
  10.    Args:
  11.        filespec (string): directory path and filespec, ex: /tmp/*.pdf
  12.        day (datetime.date): any files older than this date match
  13.    """
  14.  
  15.     for fn in glob.glob(filespec):
  16.         stamp = dt.datetime.fromtimestamp(os.path.getmtime(fn)).date()
  17.         if stamp < day:
  18.             os.remove(fn)
  19.  
  20. """ I love the langeage
  21. From the datetime library using the datetime object can call the fromtimestamp function
  22. to convert a timestamp into a date-time value and then truncate that date-time value to a date value
  23. """
  24.  
  25. # Example Run:
  26. threshhold = makeday('2017-02-15')
  27. delete_obsolete_slides('/tmp/*.png", threshhold)
Advertisement
Add Comment
Please, Sign In to add comment