Python返回存储在目录中的最近csv文件的文件路径/文件名 [英] Python return filepath/filename of most recent csv file stored in directory

查看:707
本文介绍了Python返回存储在目录中的最近csv文件的文件路径/文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹csv文件存储在。我如何让python搜索此文件夹,并返回最近创建的文件的文件名。例如搜索C:\CSVfiles并以C:\CSVfiles\CSVmostrecent.csv的形式返回文件名?我正在使用窗口。

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.

推荐答案

您可以使用参数到 max()函数:

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)

根据您的意图,您可能需要使用 os.path.getctime ,而不是 os.path.getmtime

Depending on your intention, you might want to use os.path.getctime instead of os.path.getmtime.

这篇关于Python返回存储在目录中的最近csv文件的文件路径/文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆