【问题标题】:How to retrieve the most recently created files in a directory如何检索目录中最近创建的文件
【发布时间】:2014-10-17 05:57:07
【问题描述】:

我想检索一个目录中最新创建的文件的数组。每组文件都是在同一时间段内创建的 - 间隔为 1000 毫秒。

此列表中的前 4 个文件是在相同的 1000 毫秒内创建的。我只想检索这 4 个文件:

sceflh.jpg - 2014-08-23 05:07:46.100000000
rgxanx.jpg - 2014-08-23 05:07:45.900000000
byoiup.jpg - 2014-08-23 05:07:45.700000000
rrqgnh.jpg - 2014-08-23 05:07:45.500000000
sqthcv.jpg - 2014-08-23 05:07:40.320000000
ebrmvv.jpg - 2014-08-23 05:07:40.200000000
xzvsnt.jpg - 2014-08-23 05:07:40.110000000
ckiinz.jpg - 2014-08-23 05:07:40.100000000

我将如何检索此类列表?这就是我所拥有的,它只是给了我目录中的所有文件:

def get_files(directory):
    files = []
    for file in [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory,f))]:
        files.append( '{0}/{1}'.format(directory, file) )
    return files

编辑:这是我的最终代码

import os

def get_recent_files(directory, threshold=0.9):
    files = sorted(get_files(directory), key=os.path.getmtime,reverse=True)
    filtered = filter(lambda x: os.path.getmtime(files[0]) - os.path.getmtime(x) <= threshold, files)
    return filtered

def get_files(directory):
    files = []
    for file in [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]:
        files.append( '{0}/{1}'.format(directory, file) )
    return files

for file in get_recent_files("images", 0.900):
    print(file)

【问题讨论】:

  • 你在哪个操作系统上?
  • 如果您指的是上次修改文件的时间,那么您就有机会了。但是,大多数文件系统不存储文件的创建时间,根本无法按照您的要求进行操作。
  • @WilliamPursell 好的,最后修改它。

标签: python arrays linux file directory


【解决方案1】:

您可以使用os.path.getmtimeos.path.getmtime按 linux 上的修改日期/windows 上的创建日期对文件进行排序

sorted(get_files("."),key = os.path.getmtime,reverse = True)[:5]

仅获取文件名:

[os.path.split(x)[1]  for x in  sorted(get_files("."),key = os.path.getmtime,reverse = True)[:5]]

使用您的原始功能和阈值:

files =  sorted(get_files("."), key=os.path.getmtime,reverse=True)
filtered =  filter(lambda x: os.path.getmtime(files[0]) - os.path.getmtime(x) <= .09,files)

要匹配您的问题:

导入操作系统 从日期时间导入日期时间

def get_files(directory):
    files = []
    for file in [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory,f))]:
        da = datetime.fromtimestamp(os.path.getmtime(file))
        files.append( '{0}/{1}-{2}'.format(directory,file,da)) # add time from timestamp
    return files

print sorted(get_files("."),key=lambda x: float(x.rsplit(":",1)[-1]),reverse = True)[:5] # sort based on seconds/milisecs

【讨论】:

  • 您是如何获得包含创建时间的文件列表的?我认为这就是问题所在。
  • @mnjeremiah 其实我只需要文件名。
  • @NinjaFart,第一个示例将从最近修改的文件中排序并返回文件
  • @PadraicCunningham 谢谢。你介意解释一下我将在哪里/如何调整时间跨度 - 假设我只想要在 900 毫秒内创建的最新 文件
  • 好的,所以你希望它们在一个阈值之内,我会在几分钟内添加到我的答案中
【解决方案2】:

获取文件列表及其 ctimes(或 mtimes)然后对其进行排序:

a = [(os.path.getctime(f), f)for f in os.listdir(os.curdir)]
a.sort()

过滤列表中在最近文件一秒内创建的文件

most_recent = a[-1][0]
b = [thing for thing in a if most_recent - thing[0] <= 1]

【讨论】:

  • 您正在做某事,但无法使其与子目录一起使用。你的变量命名也有点混乱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-19
  • 2022-06-11
  • 2015-10-31
相关资源
最近更新 更多