python - 如何使用python读取目录的所有新文件?
问题描述
我是 Python 的初学者,我想知道如何在此代码中添加条件以仅读取.../data/
目录的所有新文件(例如从 24 小时前开始)或(从上次执行时间开始)。.xml
因为我每天都解析我的文件,并且它再次解析所有旧文件,这需要时间。
from lxml import etree as ET
import glob
import sys
import os
path = '/home/sky/data/'
for filename in glob.glob(os.path.join(path, '*.xml')):
try:
tree = ET.parse(filename)
root = tree.getroot()
#other codes here
except Exception:
pass
谢谢!
解决方案
for filename in glob.glob(os.path.join(path, '*.xml')):
if os.path.getmtime(filename) < time.time() - 24 * 60 * 60: # 24h ago
continue # skip the old file
...
推荐阅读
- c# - 在构造函数中执行任务
- parameters - NSubstitute:当函数参数为byte[]/class时,为什么mock函数调用返回null?
- java - 将子项目添加到要在 jooq-codegen-maven 中使用的 maven 项目中
- sql - SQL INSERT 不适用于 OUTPUT INSERTED 语句
- qt - 从 Qt 获取 SO_RCVBUF 套接字选项值
- mysql - MYSQL 对两列求和
- javascript - 使用数组过滤 CSV 的行以显示 CSV 中的 arrayItem : 1
- c - 如何替换 Glade3/GTK3 代码中的 lookup_widget?
- javascript - 使用 vue-select 处理自定义模糊和焦点事件
- javascript - 使用 ajax get 方法提交表单以使用 XMLhttpreq 返回响应