I have one directory which contains below files for example purpose.

file:input/dir/input_data_Export_20190405121071.csv file:input/dir/data_Export_20190406081001.csv file:input/dir/input_Export_20190406161241.csv file:input/dir/raw_data_Export_20190407121041.csv file:input/dir/input_data_Export_20190407101101.csv

Here how can I retrieve a file which starts with "2019040712" and it should have the latest timestamp using Python. For example "input_data_Export_20190407101101.csv" is the file I want to retrieve.

1 Answers

A.Ben On

this code is giving you the name of the file with the latest timestamp. You must replace [$YOUR_PATH] by the path to the directory where your files are.

import glob

timestamp_temp = 0

for filename in glob.glob("[$YOUR_PATH]/input_data_Export_2019040712*.csv"):
    timestamp = filename[-18:-4] 
    if int(timestamp) > int(timestamp_temp):