[Tutor] comparing dates and file access

Michael P. Reilly arcege@shore.net
Mon, 15 Jan 2001 20:40:18 -0500 (EST)

> I need compare the last access file date with a today date. I only
> allow to access the file, if and just if,  todays date is  5 days
> after the last access to the file. Any help?

You can use the os.path.getatime() (or you can get it directly out of
os.stat).  Then compare that integer with one from "today".

def can_I_read(filename, when=5*24*60*60):
  import os, time
  access_time = os.path.getatime(filename)
  now = time.time()
  return (access_time + when > now)

If the function returns true, then the file is less than five days old
(five days is 5*24*60*60).

You might also want to look into the mxDateTime module (<URL:


| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |