2013-07-01 94 views
-2

我想從特定路徑檢索文件,這些文件的日期位於某個開始日期和結束日期之間。檢索日期在兩個日期之間的文件

例如:

def fetch(path, startDate, endDate): 
    """ 
    path -> path of the directory 
    startDate -> date as listed in ls (Jun 27) 
    endDate -> date as listed in ls 
    """ 

另外,我不能存儲的ls -lrt輸出,以檢查是否有特定圖案。

+2

歡迎堆棧溢出!看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出和實際獲得的輸出(控制檯輸出,堆棧跟蹤,編譯器錯誤 - 無論是適用)。您提供的細節越多,您可能會收到的答案就越多。 –

回答

1

這是一個功能,可以幫助你:

from datetime import datetime 
from os import path 
from glob import glob 
from time import time as current_time 

def get_files(pattern, start=0, end=None): 
    """ 
    returns a list of all files in pattern where the files creation date is between start and end 
    pattern = the pattern to retrieve files using glob 
    start = the start date in seconds since the epoch (default: 0) 
    end = the end date in seconds since the epoch (default: now) 
    """ 
    start = datetime.fromtimestamp(start) 
    end = datetime.fromtimestamp(current_time() if end is None else end) 
    result = [] 
    for file_path in glob(pattern): 
     if start <= datetime.fromtimestamp(path.getctime(file_path)) <= end: 
      result.append(file_path) 
    return result 

例子:

>>> get_files('C:/Python27/*') 
['C:/Python27\\DLLs', 'C:/Python27\\Doc', 'C:/Python27\\include', 'C:/Python27\\Lib', 'C:/Python27\\libs', 'C:/Python27\\LICENSE.txt', 'C:/Python27\\NEWS.txt', 'C:/Python27\\python.exe', 'C:/Python27\\pythonw.exe', 'C:/Python27\\README.txt', 'C:/Python27\\tcl', 'C:/Python27\\Tools'] 
+0

謝謝你。這真的很有幫助 – Matt

+0

如果這對你有幫助,你可以[接受我的回答](http://meta.stackexchange.com/a/5235/192545) –

+0

我在'ls -lrt'ie中查找日期; -rw-rw-r-- 1 Matt Matt 4664 Jun 12 13:53 prog.py – Matt

相關問題