第二最新文件 [英] Second newest file

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

问题描述

我需要第二个最新的文件。

I need the second newest file.

在此线程中找到最新的文件:

In this thread the newest is found:

Python获取具有特定扩展名的目录中的最新文件

,它使用以下结构:

newest = min(glob.iglob('upload/*.log'), key=os.path.getctime)

但是,如何获得最小值或最大值而不是第二个元素?

However, how can I get not the min or max but the second element?

推荐答案

我认为这可能是合适的解决方案:

I think this can be a suitable solution:

# for the min + 1
sorted(glob.iglob('*.log'), key=os.path.getctime)[1]

# for the newest
sorted(glob.iglob('*.log'), key=os.path.getctime)[-1]

# for the second newest ( max - 1)
sorted(glob.iglob('*.log'), key=os.path.getctime)[-2]

所以基本上 glob.iglob('*。log')只是一个数组(更确切地说,它的结果是一个生成器)-您可以按ctime对其进行排序并找到所需的内容。

So basically glob.iglob('*.log') is just an array (to be more precise it result is a generator) - you can sort it by ctime and find what you want.

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

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