[英]Python select last 12 hours new generated files in a folder
I am trying to get all files for last 12 hours, file names and the list have following format:我试图获取过去 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
This is my code:这是我的代码:
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)
However, this code will return all files in the folder.但是,此代码将返回文件夹中的所有文件。 I'm thinking to use below code to identify the file modified time:
我正在考虑使用以下代码来识别文件修改时间:
last12HourDateTime = datetime.today() - timedelta(hours = 12)
So that all files with last 12 hours from now on will be listed in my file_list.因此,从现在起过去 12 小时内的所有文件都将列在我的 file_list 中。 Could anybody help with this, how to integrate the time sorting for file list?
任何人都可以帮助解决这个问题,如何整合文件列表的时间排序? Much appreciated!
非常感激!
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.