Python:在目录中查找带有.MP3扩展名的最新文件 [英] Python: Find newest file with .MP3 extension in directory

查看:134
本文介绍了Python:在目录中查找带有.MP3扩展名的最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在Python中找到最近修改过的(从这里出来的'最新')一个特定类型的文件。我现在可以得到最新的,但是什么类型并不重要。我只想得到最新的MP3文件。

I am trying to find the most recently modified (From here on out 'newest') file of a specific type in Python. I can currently get the newest, but it doesn't matter what type. I would like to only get the newest MP3 file.

目前我有:

Currently I have:

import os

newest = max(os.listdir('.'), key = os.path.getctime)
print newest

有没有办法修改这只给我只有最新的MP3文件?

Is there a way to modify this to only give me only the newest MP3 file?

推荐答案

使用 glob.glob

import os
import glob
newest = max(glob.iglob('*.[Mm][Pp]3'), key=os.path.getctime)

这篇关于Python:在目录中查找带有.MP3扩展名的最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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