2014-02-25 2 views
0

안녕하세요 마지막으로 수정 된 시간에 디렉토리에있는 파일을 나열하려면 다음 코드를 사용하고필터 파일

searchdir = args[0] 

files = filter(os.path.isfile, os.listdir(searchdir))) 
files = [os.path.join(searchdir, f) for f in files] 
files.sort(key=lambda x: os.path.getmtime(x)) 
print file 

난 단지 거기에 마지막과 함께 특정 패턴의 파일을 나열하고 싶습니다 modifiedtime 위의 코드에서 특정 패턴 (파일 이름) 검색을 어떻게 얻을 수 있습니까? Mtuberc라는 파일 이름으로 모든 파일을 원하십니까?

예제 파일 :

streptococcous.log 
baccidius.log 
Mtuberc.log 
Mtuberc.log.1 
Mtuberc.log.16 
Mtuberc.log.13 

예상 출력 :

Mtuberc.log 
Mtuberc.log.1 
Mtuberc.log.16 
Mtuberc.log.13 
+0

프로그래밍 언어 란 무엇입니까? 파이썬? – Ali

+0

@Ali 예 Python에서 – user3284648

+0

좋아요, 당신이 그에 따라 태그를 붙인 것을 봅니다. 나중에 프로그래밍 언어 용 태그도 포함 시키십시오. 그러면 응답 속도가 빨라지고 질문의 가시성이 향상됩니다. 행운을 빕니다! – Ali

답변

0

아래 코드는 지정된 패턴과 일치하는 파일을 찾습니다. 사용 된 패턴은 파일 이름이 'Mtuberc.log'로 시작해야합니다.

import os 
import re 

searchdir = args[0] 
pattern = re.compile(r'^Mtuberc\.log') # whatever you want to match 
files = filter(os.path.isfile, os.listdir(searchdir)) 
files = [os.path.join(searchdir, f) for f in files if pattern.search(f)] 
files.sort(key=lambda x: os.path.getmtime(x)) 
print files