如何使用 python 读取目录的所有新文件?
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
...
我是 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
...