2013-07-01 3 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 
    """ 

또한 I는 특정 패턴을 검사하기 위하여, 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'와 같이 날짜를 찾고있었습니다. 즉; -rw-rw-r-- 1 Matt Matt 4664 Jun 12 13:53 prog.py – Matt

관련 문제