compare dictionary values

Tim Williams (gmail) tdwdotnet at gmail.com
Tue Jan 3 06:16:10 EST 2006


On 30/12/05, rbt <rbt at athop1.ath.vt.edu> wrote:
>
> What's a good way to compare values in dictionaries? I want to find
> [snip]
> My key-values pairs are filepaths and their modify times. I want to
> identify files that have been updated or added since the script last ran.


You don't need to store each file's updated time, you only need to store the
last time you ran the script and to compare each file's modified time
against that last-run time.   Dictionaries aren't required

eg:

 #    to get files changed (added/modified) since a given date
import time, os
last_run = time.time() - (60*60*24*30*12)   # for testing, set to one year
ago
changed = [x for x in  os.listdir('c:/python24') if os.path.getmtime(
os.path.join('c:/python24' , x)) > last_run]
# script end

>From your previous posts, I believe you are storing details of *every* file
within a dictionary that is pickled to your hard disk,  then periodically
creating a new dict  of *every* file and comparing it against the original
dict.   Then you must be updating the original dict with the new times and
storing it back to disk.   The above list comprehension will give the same
result for files/dirs in a single directory,  you just need to store the
time of the last script run to disk instead.

if you are walking several directories, the principle is the same,

>>> last_run = time.time() - (60*60*24*30)  # 1 month ago
>>> for root, dirs, files in os.walk('c:/python24'):
...     for name in files:
...         if os.path.getmtime(os.path.join(root , name)) > last_run:
...             print os.path.join(root , name)
...
c:/python24\Lib\asynchat.pyc
c:/python24\Lib\calendar.pyc
c:/python24\Lib\gzip.pyc
c:/python24\Lib\imghdr.pyc
c:/python24\Lib\SimpleHTTPServer.pyc
c:/python24\Lib\sndhdr.pyc
c:/python24\Lib\webbrowser.pyc
c:/python24\Lib\_strptime.pyc
c:/python24\Lib\email\MIMEAudio.pyc
c:/python24\Lib\email\MIMEImage.pyc
c:/python24\Lib\email\MIMEMessage.pyc
c:/python24\Lib\email\MIMEMultipart.pyc
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060103/1e74f568/attachment.html>


More information about the Python-list mailing list