简体   繁体   中英

Python return filepath/filename of most recent csv file stored in directory

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. eg search C:\\CSVfiles and return filename in the form of C:\\CSVfiles\\CSVmostrecent.csv? I'm using windows.

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 .

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