简体   繁体   English

无论如何要在过去 24 小时内修改文件而不遍历目录中的所有文件

[英]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.

相关问题 如何在 os 目录中获取过去 24 小时内创建的所有文件 - How to get all the files created in last 24 hours in os directory 如何获取目录 Python 中最后 4 个修改的文件 - How to get the last 4 modified files in directory Python 传输新文件或最近 24 小时内编辑过的文件 - Transfer files that are new or edited in the last 24 hours 循环遍历 Python 中的文件目录 - Looping through a directory of files in Python 在Python中循环使用大文件需要数小时 - Looping through big files takes hours in Python 使用 Python,递归地为过去 24 小时内创建的 all.jpg 文件创建符号链接 - Using Python, recursively create symbolic links for all .jpg files created within the last 24 hours Python:列出最近24小时内创建的具有特定扩展名的子目录中的文件 - Python: List files in subdirectories with specific extension created in last 24 hours 如何创建 python 脚本,以便当目录中的 csv 文件在过去 24 小时内未更新时发送 email? - How do I create a python script such that it sends an email when csv files in a directory has not updated in the last 24 hours? Python遍历文件夹中的所有文件 - Python-Looping through all files in a folder 按文件名对文件进行分组,并根据最后修改时间获取最新文件 - Grouping files by their file name and get the latest files based on the last modified
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM