Search code examples
pythonpython-3.xfilestat

How to count file in function of their age in Python?


I'm trying to count how many file are very young, young, old and very old in a directory passed by command line. I have some struggle counting the number of file in the directory and then to change the counter in function of the age of the file.

Here is what I did yet:

import sys
import os, os.path
import time

x = 7
file_count = 0
DIR = sys.argv[1]
age = 0
age_sum = 0
nb_vy = 0
nb_y = 0
nb_o = 0
nb_vo = 0
now = time.time()

for root, subFolders, files in os.walk(DIR):
    for file in (files):
        try:
            file_count += 1
            # here I want to do some if to add 1 in the counter nb_vy/nb_y/nb_o/nb_vo but I don't know how  
            age=now - os.stat(os.path.join(root,file)).st_mtime
            age_sum+=age
            if  now-timedelta(hours=24) <= age <= now :
                nb_vy += 1
            elif now-timedelta(days=7) <= age :
                nb_y += 1
            elif now-timedelta(days=30) <= age :
                nb_o += 1
            else:
                nb_vo += 1
        except Exception:
            pass


print("Filecount = %s" % file_count)


print("--------------------------------------------------------------------\n")
print("Scanned:  "+  str(file_count) +" files\n")
print("Average age:  "+  str(age/age_sum) + "\n")
print("--------------------------------------------------------------------\n")
print("Very young files (<=  1  day) |  "+  str(nb_vy/file_count) +"% (" + str(nb_vy) + ")\n")
print("Young files      (<=  1 week) |  "+  str(nb_y/file_count) +"% (" + str(nb_v) + ")\n")
print("Old files        (<= 30 days) |  "+  str(nb_o/file_count) +"% (" + str(nb_o) + ")\n")
print("Very old files   (>  30  days |  "+  str(nb_vo/file_count) +"% (" + str(nb_vo) + ")\n")
print("--------------------------------------------------------------------\n")

How can I manage the if cascade to increment the right counter ?


Solution

  • You had the sides of the comparison swapped and the unneeded now - was still there. Once those are fixed and the timedelta converted to a duration of seconds for comparison:

            if  age <= timedelta(hours=24).total_seconds():
                nb_vy += 1
            elif age <= timedelta(days=7).total_seconds():
                nb_y += 1
            elif age <= timedelta(days=30).total_seconds():
                nb_o += 1
            else:
                nb_vo += 1
    

    You should be using age < max_age_for_group as the condition. age is already now - mtime. age is in seconds.

    Also except Exception: pass will harm debugging. If you must have it at least use:

    except Exception:
        logging.exception('')
    

    This will eat the exception but still print it. And then you can turn the printing off by changing the level on the root logger.