2014-01-17 66 views
0

开始一个新的线程,最新的文件我有这种格式的文件的目录:获得基于文件名蟒蛇

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年档案,因为12-16-2013年的12比01-16-2014年的01高。

任何意见将非常感激。 谢谢

+0

请详细说明**把我扔掉**? –

+0

我认为最好是编辑标题来表示你的问题。像“比较日期在Python” – Elisha

回答

2
  • 提取日期字符串。
  • 将其转换为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 
+0

这是完美的。非常感谢。但我有什么替代方法,而不是max()?如果我认为必须找到最后两个而不是最后一个,或者如果我想找到倒数第二个? – Eric

+1

@Eric,使用'sorted'或'list.sort'来处理这种情况。 (在这之前,你需要删除重复的日期..,最好使用'set')。 – falsetru

+0

的含义,将“生成日期”从生成器更改为列表并对其进行排序? – Eric

0

使用.split("-")功能。 像

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

然后进行某种形式的,并从文件名的最新

0

您可以使用自己的比较功能可以根据你的逻辑

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 
+0

''key'参数的味道应避免使用'cmp'参数。 'cmp'涉及更多的比较(通常比较慢)。它已经在Python 3.x中消失了。 – falsetru

+0

我必须承认我不了解你的评论。你是什​​么意思“关键的味道”和“涉及更多的比较”? – Elisha

+1

请参见['sorted'](http://docs.python.org/2/library/functions.html#sorted)。 (我给你'sorted'函数的链接而不是'list.sort',因为文档中没有直接链接到'list.sort'方法,但它们有相似的参数) – falsetru