[Tutor] Program to report if file was modified today

Alan Gauld alan.gauld at btinternet.com
Thu Feb 12 09:59:17 CET 2009


"David" <david at abbottdavid.com> wrote

> I really only want it to report when a file has been modified so 
> this seems to work,

> #!/usr/bin/python
> import sys
> import os
> import time
>
> def mod():
>     """Find files modified today, given a file path."""
>     latest = 0
>     now = time.strftime('%Y-%m-%d', time.localtime())
>     dir = os.path.dirname(sys.argv[1])
>     for fname in os.listdir(dir):
>         if fname.endswith('.py'):
>             modtime = os.stat(os.path.join(dir, fname)).st_mtime
>             if modtime > latest:
>                 latest = modtime

Why do you modify latest to whatever the mod time of the
file is? Surely you want to always compare mod time with now?

>                 out = time.strftime('%Y-%m-%d', 
> time.localtime(latest))
>                 if out == now:
>                     print fname, "has changed today. "
>                 else:
>                     pass

You don't need the else:pass bit. It does nothing.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list