2014-01-17 65 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