1

I have a list of files stored in my Data folder. I can get list_of_files:

list_of_files = glob.glob(r'C:\Users\Desktop\Data\*.csv')

I want to use pandas to read the last modified file on one specific date, say 05/31/2021. On 05/31/2021, there might be more than one files modified, and I need the last modified one latest_file, then import to python script like this:

df = pd.read_csv(latest_file, usecols=['A', 'B', 'C'])

How can I realize it? Many thanks

(Any solution for the previous part would be great. It would be even better if you can make the modified date be last day of a certain month, like 04/30/2021, 05/31/2021, 6/30/2021 etc.)

Cyan
  • 319
  • 2
  • 8
  • 1
    Does [this](https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python) help? – ShlomiF Jul 06 '21 at 19:34
  • It's not the same. I don't need the modified date. I need the file – Cyan Jul 06 '21 at 19:50
  • And it is not last modified based on system time. I need the last modified based on a specific date – Cyan Jul 06 '21 at 19:52

1 Answers1

1

I would like to thank Umar.H for helping me with this issue through a call. I attached my code below:

def get_latest_file(datetime: str, location: str) -> str:
    files = Path(location).glob('*.csv')

    file_df = pd.DataFrame({'path': files})

    file_df['mdate'] = pd.to_datetime(file_df['path'].apply(lambda x: x.stat().st_mtime), unit='s')

    try:
        idx = file_df.loc[
            (file_df['mdate'] >= pd.Timestamp(datetime))
            &
            (file_df['mdate'] < (pd.Timestamp(datetime)
                                 + pd.DateOffset(hours=23, minutes=59))
             )]['mdate'].idxmax()

        print(f"Returning file {file_df.loc[idx]['path'].stem} with a modify time of {file_df.loc[idx]['mdate']}")
        return file_df.loc[idx]['path']
    except (KeyError, FileNotFoundError):
        return ("No file matched the time delta.")


df4 = pd.read_csv(get_latest_file('28 May 2021', r'C:\Users\...\Desktop'])
Cyan
  • 319
  • 2
  • 8