python:which file is newer&通过多少时间 [英] python: which file is newer & by how much time

查看:197
本文介绍了python:which file is newer&通过多少时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个归档比较例程。我怀疑以下是一个相当笨重的方法。

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

我有一些困难找到关于timedelta的属性或方法,或任何他们被称为的信息;因此,我测量下面的日期时间差异只在天,分钟和秒,而没有列表项表示年。

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


推荐答案

已经有一个解决方案:

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

现在,从时代开始,你有时间在几秒钟之内。为什么要创建一个新的表示,你可以创建一个deltatime或任何东西,为什么发明一个新的格式?

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?

这篇关于python:which file is newer&通过多少时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆