你可以使用Python的os模块来实现按修改时间查找文件的功能。以下是一个示例代码:
import os
def find_recent_files(root_folder):
files = []
for root, dirs, filenames in os.walk(root_folder):
for filename in filenames:
file_path = os.path.join(root, filename)
if os.path.isfile(file_path):
files.append((file_path, os.path.getmtime(file_path)))
files.sort(key=lambda x: x[1], reverse=True)
return [file[0] for file in files]
root_folder = "/path/to/root/folder"
recent_files = find_recent_files(root_folder)
for file in recent_files:
print(file)
在这个示例中,find_recent_files
函数接受一个根文件夹路径作为参数,并使用os.walk
遍历整个文件夹及其子文件夹。在每个文件夹中,它遍历所有文件,获取文件的修改时间戳,然后将文件路径和时间戳存储在一个列表中。
最后,使用sort
函数对文件列表进行排序,按照时间戳降序排列。然后返回只包含文件路径的列表。
你可以将root_folder
替换为你想要查找文件的根文件夹路径。代码将返回最近修改的文件路径列表,并打印出来。你可以根据自己的需要对结果进行进一步处理。