1

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 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.