這是一個功能,可以幫助你:
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']
歡迎堆棧溢出!看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出和實際獲得的輸出(控制檯輸出,堆棧跟蹤,編譯器錯誤 - 無論是適用)。您提供的細節越多,您可能會收到的答案就越多。 –