简体   繁体   English

如何使用python读取目录的所有新文件?

[英]How to read all new files of a directory with python?

I'm beginner in Python and I'm wondering to know how can I add a condition in this code to read only all new files of .../data/ directory (for example from 24 hours ago) or (from last execution time). 我是Python的新手,我想知道如何在此代码中添加条件,以仅读取.../data/目录的所有新文件(例如,从24小时前开始)或(从上次执行时间开始) )。 Because I parse my .xml files every day and it is parsing all old files again and it takes time. 因为我每天都解析.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

Thanks! 谢谢!

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
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM