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

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

内容简介:翻译自: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


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

查看所有标签

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

The Sovereign Individual

The Sovereign Individual

James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00

Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具