3

I am using below to remove files from disk.

    def match_files(dir, pattern):
       for dirname, subdirs, files in os.walk(dir):
          for f in files:
             if f.endswith(pattern):
                yield os.path.join(dirname, f)

    # Remove all files in the current dir matching *.txt
    for f in match_files(dn, '.txt'):
       os.remove(f)

What I would to remove files from disk that "was not updated today." List the files from today. Check against to update list.

Merlin
  • 24,552
  • 41
  • 131
  • 206

2 Answers2

3

Besides os.stat you could use os.path.getmtime or os.path.getctime, the pro's / con's of which are discussed on this question. You can use datetime.datetime.fromtimestamp to convert the timestamp returned into a datetime object, and then you can do whatever you want. In this example I'll remove files not modified today, create a list of remaining files:

from datetime import datetime, timedelta

today = datetime.now().date()
remaining = []
for f in match_files(dn, '.txt'):
   mtime = datetime.fromtimestamp(os.path.getmtime(f)).date()
   if mtime != today:
       os.remove(f)
   else:
       remaining.append(f)
Community
  • 1
  • 1
Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
  • if the logic is not "yesterday" but rather not today. so deleting everything that was not updated today....in case of weekends skipped days. – Merlin Sep 02 '11 at 15:35
  • Your question is actually "remove files from disk that are from yesterday", if you want to extend it to everything that was not updated today, you can do so pretty easily, just do `today = datetime.now().date(); if mtime != today:`. Adjust the logic as necessary. – Zach Kelling Sep 02 '11 at 15:38
  • thanks, for fixing logic..... but how would I list the remain files. the files would look like...Foo.txt, bar.txt....would like list ['foo', 'bar',] – Merlin Sep 02 '11 at 15:49
  • If `mtime == today` it is a remaining file. – Zach Kelling Sep 02 '11 at 15:50
  • I know the remain files were updated today. But need to list them. So, I compare them against a new list of files that may need to be updated. not the whole file just ['Foo', 'bar',] – Merlin Sep 02 '11 at 15:57
  • My example appends them to the `remaining` list, which you can use for comparisons later. – Zach Kelling Sep 02 '11 at 15:59
  • Should i strip out .txt for append or after remaining list has been built – Merlin Sep 02 '11 at 16:04
  • If you need to strip it, it makes sense to strip it before you append to the list. – Zach Kelling Sep 02 '11 at 16:06
  • any thoughts code to strip extensions of file. I am nervous about testing code when deleting/removing files. I hope you understand. – Merlin Sep 02 '11 at 16:21
  • You can do `filename.rsplit('.', 1)[0]`, which splits on the first period from the right, `[0]` being the filename w/o extension. In regards to your concerns about testing code while deleting/removing, indeed that's a bad idea. You can always test bits of code in the interpreter first, and/or setup a dummy directory to make sure your script works how you want. – Zach Kelling Sep 02 '11 at 17:17
  • Also have a look at os.path.splitext(path), which gives you a pair with path and filename, and dot extension. – Louis Sep 02 '11 at 18:27
1

What is "pattern" ?

Otherwise, the "os.stat" gives the date of the file. Here a sample with the "last mod" date.

    stats = os.stat(file)
    lastmod_date = time.localtime(stats[8])
Louis
  • 2,854
  • 2
  • 19
  • 24