[Tutor] Program to report if file was modified today

bob gailer bgailer at gmail.com
Thu Feb 12 02:46:11 CET 2009


David wrote:
> 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>

1) That can't be the entire program, as there is no call to Mod()!
2) Convention says function names start with lower case. Save initial 
caps for class names.
3) Get rid of latest - that is why you are not getting all the files.
4) Use glob.glob() so you can specify *.py
5) It is not necessary to convert times to strings. You can get midnight 
today from int(time.localtime())
6) Move calculation of now outside the loop, as it does not change.
7) my_list is not necessary.
8) revised (untested) code:

<code>
#!/usr/bin/python
import sys
import os
import time
import glob

def mod():
    """Find files modified today, given a file path."""
    dir = os.path.dirname(sys.argv[1])
    start_of_today = int(time.localtime())
    for fname in glob.glob(dir + '/*.py'):
        modtime = os.stat(os.path.join(dir, fname)).st_mtime
        print fname
        if modtime >= start_of_today:
            print "This file has changed today."
            print "*"*30
        else:
            print "This file did not change"
mod()
</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
>
>


-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list