如何获取文件夹中的最新文件? [英] How to get the latest file in a folder?

查看:82
本文介绍了如何获取文件夹中的最新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 python 获取文件夹的最新文件.使用代码时:

I need to get the latest file of a folder using python. While using the code:

max(files, key = os.path.getctime)

我收到以下错误:

FileNotFoundError: [WinError 2] 系统找不到指定的文件:'a'

推荐答案

分配给 files 变量的内容不正确.使用以下代码.

Whatever is assigned to the files variable is incorrect. Use the following code.

import glob
import os

list_of_files = glob.glob('/path/to/folder/*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)

这篇关于如何获取文件夹中的最新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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