[英]How to read all new files of a directory with 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
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.