Skip to content Skip to sidebar Skip to footer

How To Compare The Modified Date Of Two Files In Python?

I am creating a python script that will access each line from a Text file(say File.txt) one by one then search for corresponding '.py' and '.txt' file in the system directory. For

Solution 1:

time.ctime() formats a time as a string, so you're comparing the strings "Fri Feb 08 16:34:43 2013" and "Sat Sep 22 14:19:32 2012" textually. Just don't do that and compare the floats that getmtime() gives you directly:

pytime = os.path.getmtime(os.path.join(root, sc))
# ...
txttime = os.path.getmtime(os.path.join(root, txt))
# ...
if (txttime > pytime):
    # ...

Solution 2:

time.ctime returns a string and 'Fri Feb 08 16:34:53 2013' < 'Mon Sep 24 00:50:07 2012'

Post a Comment for "How To Compare The Modified Date Of Two Files In Python?"