【问题标题】:python: which file is newer & by how much timepython:哪个文件更新以及多少时间
【发布时间】:2011-11-28 14:07:16
【问题描述】:

我正在尝试创建一个文件日期比较例程。我怀疑以下是一种相当笨拙的方法。

我很难找到有关 timedelta 的属性或方法的信息,或者它们被称为什么的信息;因此,我只用天、分和秒来测量下面的日期时间差,并且没有代表年的列表项。

任何关于替代方案的建议,将不胜感激。

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

【问题讨论】:

  • 真的很难说。 filepath1e 和 filepath2e 是什么类型?

标签: python comparison timedelta


【解决方案1】:

已经有解决方案了:

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

现在您有了自 Epoch 以来以秒为单位的时间。你为什么要创建一个新的表示,你可以创建一个 deltatime 或其他什么,为什么要发明一种新格式?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 2011-04-23
    • 1970-01-01
    • 2016-07-15
    • 2018-02-23
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多