简体   繁体   中英

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? 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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM