[英]Python select last 12 hours new generated files in a folder
我試圖獲取過去 12 小時內的所有文件,文件名和列表具有以下格式:
C:\myproject\Attendance_aaa_2021-07-22-4hr.csv 07/22/2021 04:20 AM Modified
C:\myproject\Attendance_bbb_2021-07-23-16hr.csv 07/23/2021 16:20 PM Modified
C:\myproject\Attendance_ccc_2021-07-26-4hr.csv 07/26/2021 04:15 AM Modified
C:\myproject\Attendance_ddd_2021-07-26-16h.csv 07/26/2021 16:15 AM Modified
C:\myproject\Attendance_eee_2021-07-26-16h.csv 07/26/2021 16:35 AM Modified
這是我的代碼:
import os
path_dataset = r'C:\myproject\'
def get_file(path_dataset):
files = os.listdir(path_dataset) #check file list
files.sort() #sort file
file_list = []
for file in files:
if (file.startswith("Attendance")) & (file.endswith(".csv")):
f_name = str(file)
tr = '\\'
filename = path_dataset + tr + f_name
file_list.append(filename)
return (file_list)
get_file(path_dataset)
但是,此代碼將返回文件夾中的所有文件。 我正在考慮使用以下代碼來識別文件修改時間:
last12HourDateTime = datetime.today() - timedelta(hours = 12)
因此,從現在起過去 12 小時內的所有文件都將列在我的 file_list 中。 任何人都可以幫助解決這個問題,如何整合文件列表的時間排序? 非常感激!
import os
import time
path_dataset = 'C:\\myproject\\'
twelve = time.time() - 12 * 60 * 60
def get_file(path_dataset):
files = os.listdir(path_dataset) #check file list
file_list = []
for file in files:
path = path.dataset + "\\" + file
if file.startswith("Attendance") and file.endswith(".csv") and os.path.getmtime(path) > twelve:
file_list.append(path)
return file_list
print(get_file(path_dataset))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.