检索日期介于两个日期之间的文件

编程入门 行业动态 更新时间:2024-10-28 18:27:40
本文介绍了检索日期介于两个日期之间的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从特定路径检索文件,该文件的日期介于某个开始日期和结束日期之间.

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']

更多推荐

检索日期介于两个日期之间的文件

本文发布于:2023-10-30 12:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542838.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   两个   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!