Python 监测文件是否更新的方法

yipeiwu_com6年前Python基础

主要逻辑是判断文件的最后修改时间与创建时间是否在秒级别上一致,此代码适用于Python 2.

import time
import os

#Read fime name
FileName='D:/scapegoat/xx.csv'

#print file creation time
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(FileName).st_ctime))

#print file modified time
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(FileName).st_mtime)

因为 os.stat 取出的时间为Linux的时间戳(从1970/1/1至今的秒数),不方便我们读取时间,所以会打印出转换的时间格式。

由于Linux时间戳精度太高,我们只保留到秒级别。

if int(os.stat(FileName).st_ctime)==int(os.stat(FileName).st_mtime):
  print 'File has not been modified.'

以上这篇Python 监测文件是否更新的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python+pandas分析nginx日志的实例

python+pandas分析nginx日志的实例

需求 通过分析nginx访问日志,获取每个接口响应时间最大值、最小值、平均值及访问量。 实现原理 将nginx日志uriuriupstream_response_time字段存放到pan...

Django中使用locals()函数的技巧

对 current_datetime 的一次赋值操作: def current_datetime(request): now = datetime.datetime.now()...

Django ORM 常用字段与不常用字段汇总

Django ORM 常用字段与不常用字段汇总

常用字段 AutoField: int 自增列,必须填入参数 primary_key=True 如果没有写 AutoField,则会自动创建一个列名为 id 的列 from dja...

python3使用flask编写注册post接口的方法

使用python3的Flask库写了一个接口,封装了很多东西,仅供参考即可! 代码如下: #!/usr/bin/python3 # -*- coding: utf-8 -*- im...

使用Python给头像加上圣诞帽或圣诞老人小图标附源码

使用Python给头像加上圣诞帽或圣诞老人小图标附源码

随着圣诞的到来,想给给自己的头像加上一顶圣诞帽。如果不是头像,就加一个圣诞老人陪伴。 用Python给头像加上圣诞帽,看了下大概也都是来自2017年大神的文章:https://zh...