本文介绍了检索日期介于两个日期之间的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从特定路径检索文件,该文件的日期介于某个开始日期和结束日期之间.
I want to retrieve files from a particular path, whose dates fall in between some start and end date.
例如:
def fetch(path, startDate, endDate): """ path -> path of the directory startDate -> date as listed in ls (Jun 27) endDate -> date as listed in ls """此外,我无法存储ls -lrt的输出,以检查特定的模式.
Also, I am unable to store the output of ls -lrt, so as to check for a particular pattern.
推荐答案此功能可能会为您提供帮助:
This is a function that might help you:
from datetime import datetime from os import path from glob import glob from time import time as current_time def get_files(pattern, start=0, end=None): """ returns a list of all files in pattern where the files creation date is between start and end pattern = the pattern to retrieve files using glob start = the start date in seconds since the epoch (default: 0) end = the end date in seconds since the epoch (default: now) """ start = datetime.fromtimestamp(start) end = datetime.fromtimestamp(current_time() if end is None else end) result = [] for file_path in glob(pattern): if start <= datetime.fromtimestamp(path.getctime(file_path)) <= end: result.append(file_path) return result示例:
>>> get_files('C:/Python27/*') ['C:/Python27\\DLLs', 'C:/Python27\\Doc', 'C:/Python27\\include', 'C:/Python27\\Lib', 'C:/Python27\\libs', 'C:/Python27\\LICENSE.txt', 'C:/Python27\\NEWS.txt', 'C:/Python27\\python.exe', 'C:/Python27\\pythonw.exe', 'C:/Python27\\README.txt', 'C:/Python27\\tcl', 'C:/Python27\\Tools']更多推荐
检索日期介于两个日期之间的文件
发布评论