如何在Python中查看文件是否超过3个月?

栏目: Python · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/5799070/how-to-see-if-file-is-older-than-3-months-in-python
我很想知道在 Python

中操纵时间.我可以使用os.path.getmtime()函数获取文件的(上次修改)年龄:

import os.path, time    

os.path.getmtime(oldLoc)

我需要运行某种测试来查看这个时间是否在过去三个月内,但我对 Python 中所有可用的时间选项感到困惑.

谁能提供任何见解?亲切的问候.

如果您需要准确的天数,可以将日历模块与日期时间结合使用,例如,

import calendar
import datetime

def total_number_of_days(number_of_months=3):
    c = calendar.Calendar()
    d = datetime.datetime.now()
    total = 0
    for offset in range(0, number_of_months):
        current_month = d.month - offset
        while current_month <= 0:
            current_month = 12 + current_month
        days_in_month = len( filter(lambda x: x != 0, c.itermonthdays(d.year, current_month)))
        total = total + days_in_month
    return total

然后将total_number_of_days()的结果提供给其他人为日期算术提供的代码.

翻译自:https://stackoverflow.com/questions/5799070/how-to-see-if-file-is-older-than-3-months-in-python


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hacking Growth

Hacking Growth

Sean Ellis、Morgan Brown / Crown Business / 2017-4-25 / USD 29.00

The definitive playbook by the pioneers of Growth Hacking, one of the hottest business methodologies in Silicon Valley and beyond. It seems hard to believe today, but there was a time when Airbnb w......一起来看看 《Hacking Growth》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具