[Tutor] Program to report if file was modified today
David
david at abbottdavid.com
Thu Feb 12 03:26:40 CET 2009
bob gailer wrote:
> 1) That can't be the entire program, as there is no call to Mod()!
Yep, it was lost in my copy paste
> 2) Convention says function names start with lower case. Save initial
> caps for class names.
Thanks for the reminder, I had forgotten :)
> 3) Get rid of latest - that is why you are not getting all the files.
For some reason when I take that out it reports all file have been modified.
> 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())
OK
> 6) Move calculation of now outside the loop, as it does not change.
Good Point, makes since now.
> 7) my_list is not necessary.
Ok, my reason was to make sure the file and date remained together but
It works fine without it.
> 8) revised (untested) code:
I get this error with the int(time.localtime())
start_of_today = int(time.localtime())
TypeError: int() argument must be a string or a number, not
'time.struct_time'
Thanks Bob,
I really only want it to report when a file has been modified so this
seems to work, next I will work on putting glob.glob working plus any
other suggestions.
<code>
#!/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
out = time.strftime('%Y-%m-%d', time.localtime(latest))
if out == now:
print fname, "has changed today. "
else:
pass
mod()
</code>
results;
david [09:25 PM] opteron ~ $ ./py_lastmod_date.py /home/david/
py_compare_time.py has changed today.
py_lastmod_date.py has changed today.
-david
--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu
More information about the Tutor
mailing list