简体   繁体   中英

Read the latest file in the folder with a specific string name

I need to open the latest file that was created with a specific string in its name. There are two files generated every 10 minutes in the folder from the database, one of them with string "XXX" and another one with "YYY". I need to open the latest file with "XXX". My current code takes a little bit more time then I was expecting, about 45 sec, and I was wondering whether somebody could suggest me how to improve it to run faster.

Here is my code:

from datetime import datetime
import pandas as pd
today_str = datetime.date.today().strftime('%m%d%Y')
x_in_strng = ["XXX"]
dt_lst = [today_str]
file_lst = []
path = "xx/xxxx/xxxx"

list_of_files = glob.glob(path + '*') 
for filename in list_of_files:
    if all(x not in filename for x in x_in_strng) and all(x in filename for x in dt_lst):
        file_lst.append(filename)
        latest_file = max(file_lst, key=os.path.getctime)
        data_df = pd.read_csv(latest_file)
        

You may find this faster:

from glob import glob, escape
import os
import pandas as pd

def get_latest_file(directory, pattern):
    glob_pattern = os.path.join(directory, f'*{pattern}*')
    ctime = float('-inf')
    file = None
    for filename in glob(glob_pattern):
        if (ct := os.path.getctime(filename)) > ctime:
            ctime = ct
            file = filename
    if file is not None:
        return pd.read_csv(file)

df = get_latest_file('basedir', 'XXX')

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