[英]Is there anyway to get files modified in last 24 hours without looping through all files in directory
Is there anyway to get files modified in last 24 hours without looping through all files in directory?无论如何要在过去 24 小时内修改文件而不遍历目录中的所有文件? The reason why I don't want to loop through all the files in directory is because the directory has over 200k files in it.
我不想遍历目录中的所有文件的原因是因为该目录中有超过 200k 的文件。
for pdf in os.scandir(ERROR_FOLDER):
path = os.path.join(ERROR_FOLDER,pdf)
filetime = dt.datetime.fromtimestamp(
os.path.getmtime(path))
if (date_start < filetime < date_end):
files.append(pdf)
There is no way to do this in general, but for specific cases it is possible.一般没有办法做到这一点,但对于特定情况是可能的。
For instance, if those files are created and modified with some automated process, you can maintain a database of files that have been modified in the past 24 hours by adding the files to the database after they have been modified (in the same script/program that modifies them), and have the database remove old entries using a cron job.例如,如果这些文件是通过某种自动化过程创建和修改的,您可以通过在修改后将文件添加到数据库中来维护过去 24 小时内修改过的文件的数据库(在同一脚本/程序中)修改它们),并让数据库使用 cron 作业删除旧条目。
Depending on how often the files are modified, you could also have a cron job that loops through all files in the directory and saves a list of the files that have been modified in the last 24 hours, so that your program runs quickly when you need it to, but may not pick up on the very latest files (that are modified between when the cron job runs and when the program runs).根据修改文件的频率,您还可以有一个 cron 作业,循环遍历目录中的所有文件并保存过去 24 小时内已修改的文件列表,以便您的程序在需要时快速运行它可以,但可能无法获取最新的文件(在 cron 作业运行和程序运行之间进行了修改)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.