[Tutor] Program to report if file was modified today

David david at abbottdavid.com
Thu Feb 12 01:42:05 CET 2009


Hi Everyone and thanks for the list and your help.
In my adventure in learning Python (never programed before) I am 
attempting to findout if a file has been modified today given a search 
path. It is working somewhat. Here it is;
<code>
#!/usr/bin/python
import sys
import os
import time

def Mod():
     """Find files modified today, given a file path."""
     latest = 0
     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
                 out = time.strftime('%Y-%m-%d', time.localtime(latest))
                 my_list = [fname, out]
                 print fname
                 now = time.strftime('%Y-%m-%d', time.localtime())
                 if my_list[-1] == now:
                     print "This file has changed today."
                     print "*"*30
                 else:
                     print "This file did not change"
</code>
results;
./py_lastmod_date.py /home/david/

py_pyparse_end.py
This file did not change
******************************
cologne_time.py
This file did not change
******************************
py_find_word_prefix.py
This file did not change
******************************
py_round_by_five.py
This file did not change
******************************
graphics.py
This file did not change
******************************
py_countdown_generator.py
This file did not change
******************************
py_compare_time.py
This file has changed today.
******************************
py_lastmod_date.py
This file has changed today.
******************************

OK here are my questions.
Is there an easier way of doing this?
Why does it only return 8 results, I have a ton of .py files in /home/david
Why do both the if and else get printed?
How do I do this without all the if statments?
I don't understand the if modtime > latest part, is this to loop over 
the files and put the oldest last? I found that part in another program 
and my program does not work without it.
Thank you,
-david


-- 
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu



More information about the Tutor mailing list