* 디렉토리가 다르면 sys.argv를 이용하여 파일명을 입력받을 수 있다.
파이썬의 glob모듈과 정규식을 이용한 다수의 파일에서 원하는 문자열 찾기
- 디렉토리 : D:\test
. 파일1 : test1.txt
AAA
BBB
CCC FFF
DDD
[AAA] AAA-
. 파일2 : test2.txt
DDD
EEE AAA.
FFF HHH
. 파일3 : test3.txt
GGG
HHHBBB
III
FFFAAA
'AAA' 이라는 문자열이 포함되어 있는 파일과 정보 추출
import glob
import re
s = str(input('Input Searching Text : '))
p = re.compile(s)
for i in glob.glob(r'D:\test\*.txt'):
with open(i, 'r') as f:
for x, y in enumerate(f.readlines(),1):
m = p.findall(y)
if m:
print('File %s [ %d ] Line Searching : %s' %(i,x,m))
print('Full Line Text : %s' %y)
print()
Input Searching Text : AAA
File D:\test\test1.txt [ 1 ] Line Searching : ['AAA']
Full Line Text : AAA
File D:\test\test1.txt [ 5 ] Line Searching : ['AAA', 'AAA']
Full Line Text : [AAA] AAA-
File D:\test\test2.txt [ 2 ] Line Searching : ['AAA']
Full Line Text : EEE AAA.
File D:\test\test3.txt [ 4 ] Line Searching : ['AAA']
Full Line Text : FFFAAA
'Python > Python for Windows' 카테고리의 다른 글
python을 이용한 IIS web log 분석 (3) - 클라이언트 IP 국가식별 (0) | 2019.07.15 |
---|---|
윈도우 성능 데이터 분석 3부 - 사례를 이용한 grafana 시각화 (0) | 2019.06.26 |
[Python] 디렉토리(하위포함) 파일명 점검 하기 (0) | 2018.07.09 |
[Python] 변경된 날짜기준 파일검색 (0) | 2018.06.17 |
[Python] pickle 모듈 - 객체형태를 그대로 유지하면서 파일에 저장하고 읽기 (0) | 2018.05.20 |
[Python] win32evtlog 모듈을 이용한 윈도우 이벤트 로그 추출 (0) | 2018.05.20 |
[Python] 리스트의 index를 활용하여 문자열 분리 (0) | 2018.05.13 |
[Python] subprocess 모듈과 DOS ping 명령어를 이용한 핼스 체크 (0) | 2018.05.09 |