【问题标题】:get latest file based on filename python根据文件名python获取最新文件
【发布时间】:2014-01-17 10:48:22
【问题描述】:

this开始一个新线程

我有一个包含这种格式文件的目录:

Report_Test-01-16-2014.09_42-en.zip
Another Report_Test-01-16-2014.09_42-en.zip
Report_Holiday-01-16-2014.09_42-en.zip
Report_Weekday-01-16-2014.09_42-en.zip
Report_Special-01-16-2014.09_42-en.zip

Report_Test-12-16-2013.10_52-en.zip
Another Report_Test-12-16-2013.10_52-en.zip
Report_Holiday-12-16-2013.10_52-en.zip
Report_Weekday-12-16-2013.10_52-en.zip
Report_Special-12-16-2013.10_52-en.zip

我无法控制文件命名并且文件名模式保持一致。 之前thread的所有方法我都试过了

我需要能够根据文件名中的日期返回最后一个文件和最后两个文件。 不幸的是,日期的 %m-%d-%Y 格式让我失望。我最终得到了 2013 年的文件,因为 2013 年 12 月 16 日的 12 高于 2014 年 1 月 16 日的 01。

非常感谢任何建议。 谢谢

【问题讨论】:

  • 请详细说明让我失望
  • 我认为最好编辑标题来代表您的问题。类似于“在 python 中比较日期”

标签: python date datetime


【解决方案1】:
  • 从文件名中提取日期字符串。
  • 将其转换为date 对象。
  • 查找最后日期。 (1)
  • 使用最后日期过滤文件名。

filenames = [
    'Report_Test-01-16-2014.09_42-en.zip',
    'Another Report_Test-01-16-2014.09_42-en.zip',
    'Report_Holiday-01-16-2014.09_42-en.zip',
    'Report_Weekday-01-16-2014.09_42-en.zip',
    'Report_Special-01-16-2014.09_42-en.zip',
    'Report_Test-12-16-2013.10_52-en.zip',
    'Another Report_Test-12-16-2013.10_52-en.zip',
    'Report_Holiday-12-16-2013.10_52-en.zip',
    'Report_Weekday-12-16-2013.10_52-en.zip',
    'Report_Special-12-16-2013.10_52-en.zip',
] # Used in place of `os.listdir(....)`

import re
import datetime

date_pattern = re.compile(r'\b(\d{2})-(\d{2})-(\d{4})\b')
def get_date(filename):
    matched = date_pattern.search(filename)
    if not matched:
        return None
    m, d, y = map(int, matched.groups())
    return datetime.date(y, m, d)

dates = (get_date(fn) for fn in filenames)
dates = (d for d in dates if d is not None)
last_date = max(dates)
last_date = last_date.strftime('%m-%d-%Y')
filenames = [fn for fn in filenames if last_date in fn]
for fn in filenames:
    print(fn)

输出:

Report_Test-01-16-2014.09_42-en.zip
Another Report_Test-01-16-2014.09_42-en.zip
Report_Holiday-01-16-2014.09_42-en.zip
Report_Weekday-01-16-2014.09_42-en.zip
Report_Special-01-16-2014.09_42-en.zip

【讨论】:

  • 这是完美的。非常感谢。但是我有什么替代方法来代替 max() ?如果我说必须找到最后两个而不是只找到最后一个,或者如果我想找到倒数第二个?
  • @Eric,在这种情况下使用sortedlist.sort。 (在此之前,您需要删除重复的日期..,最好使用set)。
  • 意思是,将“日期”从生成器更改为列表并对其进行排序?
  • @Eric, dates = sorted(set(dates)); last_two_dates = dates[-2:]
  • @Eric,我的意思是:生成器中有多个相同的日期。您可以使用set 数据结构来获取唯一日期。
【解决方案2】:

使用.split("-") 函数。 喜欢

x="Report_Test-01-16-2014.09_42-en.zip"
y=x.split("-") #['Report_Test', '01', '16', '2014.09_42', 'en.zip']

然后进行一些排序并获取最新的

【讨论】:

    【解决方案3】:

    您可以使用自己的比较函数根据您的逻辑进行比较

    filenames = ["Report_Test-01-16-2014.09_42-en.zip",
                 "Report_Special-12-16-2013.10_52-en.zip"]
    
    def compare_dates(fn1,fn2):
            # parse the date information
            day1,month1,year1 = fn1.split(".")[0].split("-")[-3:]
            day2,month2,year2 = fn2.split(".")[0].split("-")[-3:]
            ret = cmp(year1,year2) # first compare the years
            if ret != 0:
                return ret
            ret = cmp(month1,month2) # if years equal, compare months
            if ret != 0:
                return ret
            return cmp(day1,day2) # if months equal, compare days
    
    filenames.sort(cmp=compare_dates)
    

    现在 2013 年是在 2014 年之前:

    >>> filenames
    ['Report_Special-12-16-2013.10_52-en.zip', 'Report_Test-01-16-2014.09_42-en.zip
    

    【讨论】:

    • key 参数的风格中应避免使用cmp 参数。 cmp 涉及更多比较(通常较慢)。它在 Python 3.x 中消失了。
    • 我必须承认我不明白你的评论。你是什​​么意思“关键的味道”和“涉及更多的比较”?
    • sorted。 (我给你sorted函数的链接而不是list.sort,因为文档中没有直接链接到list.sort方法,但它们有相似的参数)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2018-11-09
    相关资源
    最近更新 更多