python: 哪个文件更新&需要多少时间

发布于 2024-12-18 20:52:04 字数 884 浏览 3 评论 0原文

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

我在查找有关 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2024-12-25 20:52:04

已经有一个解决方案:

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 以来以秒为单位的时间。你为什么要创建一个新的表示形式,你可以创建一个增量时间或其他任何东西,为什么要发明一种新格式?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文