I have a folder that csv files are stored in. How can I get python to search this folder and return the filename of the most recent file created. e.g search C:\CSVfiles and return filename in the form of C:\CSVfiles\CSVmostrecent.csv? I'm using windows.
1 Answer
You can use the key parameter to the max() function:
import os
import glob
filename = max(glob.iglob("c:/csvfiles/*.csv"), key=os.path.getmtime)
Depending on your intention, you might want to use os.path.getctime instead of os.path.getmtime.