Comparing file last access date

MRAB python at mrabarnett.plus.com
Sun Feb 14 19:20:27 CET 2010


kaklis at gmail.com wrote:
> Hi to all,
> what i want is to search a folder, and if the last access date of the
> files in that folder is greater than, lets say 7 days, those files
> deleted. (Somekind of a file cleaner script)
> I had problems with converting
> 
> now = today = datetime.date.today()
> and
> stats = os.stat(file)
> lastAccessDate = time.localtime(stats[7])
> 
> into matching formats so that
>      if (now - lastAccessDate) > 7:
>                  delete the file
> 
> what i do wrong
> Thanks in advance
> Antonis

I would use:

     seven_days = 7 * 24 * 60 * 60 # 7 days in seconds

     now = time.time() # in seconds since Epoch

     ...

     last_access = os.path.getatime(path) # in seconds since Epoch
     if now - last_access > seven_days:
         os.remove(path)



More information about the Python-list mailing list