我想从特定路径检索文件,其日期介于某个开始日期和结束日期之间。
例如:
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
的输出,以便检查特定模式。
答案 0 :(得分:1)
这是一个可以帮助你的功能:
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']