python: which file is newer & by how much time

白昼怎懂夜的黑 提交于 2019-12-01 03:41:43

问题


I am trying to create a filedate comparison routine. I suspect that the following is a rather clunky approach.

I had some difficulty finding info about timedelta's attributes or methods, or whatever they are called; hence, I measured the datetime difference below only in terms of days, minutes and seconds, and there is no list item representing years.

Any suggestions for an alternative, would be much appreciated.

import os
import datetime
from datetime import datetime
import sys

def datetime_filedif(filepath1e, filepath2e):
    filelpath1 = str(filepath1e)
    filepath1 = str(filepath1e)
    filepath2 = str(filepath2e)

    filepath1_lmdate = datetime.fromtimestamp(os.path.getmtime(filepath1))
    filepath2_lmdate = datetime.fromtimestamp(os.path.getmtime(filepath2))

    td_files = filepath2_lmdate - filepath1_lmdate #Time delta of the 2 filedates
    td_list = [('td_files.days', td_files.days), ('td_hrs', int(str(td_files.seconds))/3600), ('td_minutes', (int(str(td_files.seconds))%3600)/60), ('td_seconds', (int(str(td_files.seconds))%3600)%60)]

    print "Line 25: ", str(td_list)

    return td_list

回答1:


There is a solution for that already:

import os
modified_time = os.stat(path).st_mtime # time of most recent content modification
diff_time = os.stat(path_1).st_mtime - os.stat(path_2).st_mtime

Now you have the time in seconds since Epoch. why are you creating a new representation, you can create a deltatime or whatever from this, why invent a new format?



来源:https://stackoverflow.com/questions/8297003/python-which-file-is-newer-by-how-much-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!