Python在特定文件中搜索特定错误信息,但循环输出两次
Python Searching for specic error message in specific files , but the loop output twice
import os
import sys
import re
import fnmatch
from time import sleep
#The colours of the things
class bcolors:
HEADER = '3[95m'
OKBLUE = '3[94m'
OKGREEN = '3[92m'
WARNING = '3[93m'
FAIL = '3[91m'
ENDC = '3[0m'
BOLD = '3[1m'
UNDERLINE = '3[4m'
pattern = ['*.results','*.reports','*.log','*.sum','*.summary','*.spi','*.log','*.ext','*.sum','*.summary','*.spi','*.info']
search_str = "ERROR:"
mystring = search_str.encode('utf-8')
#print len(sys.argv)
print ('checking for errors in', sys.argv[1])
if len(sys.argv) < 1:
sys.exit('Usage: check_error.py <build directory>')
for x in pattern:
for path,dirs,files in os.walk(sys.argv[1]):
for fname in files:
# Test the filename for particular pattern matches.
if fnmatch.fnmatch(fname,x):
fullpath = os.path.join(path,fname)
with open(fullpath,'rb') as f:
line = f.read()
# Initialize counter for line number
line_no = 1
# Loop until EOF
if line != '' :
# Search for string in line
index = line.find(mystring.lower())
if ( index != -1) :
print(bcolors.FAIL + '[-]' + bcolors.ENDC + ' ', fname, ' ', search_str , ' ' , 'exists! Please check', ' ', fullpath, sep="" )
print(' ')
sleep(0.01)
else:
print(bcolors.OKGREEN + '[+]' + bcolors.ENDC + ' ', fname, ' ', 'OK!', sep="")
print(" ")
sleep(0.01)
# Increment line counter
line_no += 1
输出看起来像
[+] prefix.log OK!
[-] test.log ERROR: exists! Please check logs/test.log
[-] test.sum ERROR: exists! Please check logs/test.sum
[+] prefix.log OK!
[-] test.log ERROR: exists! Please check logs/test.log
[-] test.sum ERROR: exists! Please check logs/test.sum
请给出解决方法
您看到 *.log
和 *.sum
文件的输出两次,因为这些模式在您的模式列表中出现了两次。删除重复项,您将看到您期望的输出(我认为)。
pattern = ['*.results','*.reports','*.log','*.sum', ... '*.log','*.ext','*.sum', ...]
import os
import sys
import re
import fnmatch
from time import sleep
#The colours of the things
class bcolors:
HEADER = '3[95m'
OKBLUE = '3[94m'
OKGREEN = '3[92m'
WARNING = '3[93m'
FAIL = '3[91m'
ENDC = '3[0m'
BOLD = '3[1m'
UNDERLINE = '3[4m'
pattern = ['*.results','*.reports','*.log','*.sum','*.summary','*.spi','*.log','*.ext','*.sum','*.summary','*.spi','*.info']
search_str = "ERROR:"
mystring = search_str.encode('utf-8')
#print len(sys.argv)
print ('checking for errors in', sys.argv[1])
if len(sys.argv) < 1:
sys.exit('Usage: check_error.py <build directory>')
for x in pattern:
for path,dirs,files in os.walk(sys.argv[1]):
for fname in files:
# Test the filename for particular pattern matches.
if fnmatch.fnmatch(fname,x):
fullpath = os.path.join(path,fname)
with open(fullpath,'rb') as f:
line = f.read()
# Initialize counter for line number
line_no = 1
# Loop until EOF
if line != '' :
# Search for string in line
index = line.find(mystring.lower())
if ( index != -1) :
print(bcolors.FAIL + '[-]' + bcolors.ENDC + ' ', fname, ' ', search_str , ' ' , 'exists! Please check', ' ', fullpath, sep="" )
print(' ')
sleep(0.01)
else:
print(bcolors.OKGREEN + '[+]' + bcolors.ENDC + ' ', fname, ' ', 'OK!', sep="")
print(" ")
sleep(0.01)
# Increment line counter
line_no += 1
输出看起来像
[+] prefix.log OK!
[-] test.log ERROR: exists! Please check logs/test.log
[-] test.sum ERROR: exists! Please check logs/test.sum
[+] prefix.log OK!
[-] test.log ERROR: exists! Please check logs/test.log
[-] test.sum ERROR: exists! Please check logs/test.sum
请给出解决方法
您看到 *.log
和 *.sum
文件的输出两次,因为这些模式在您的模式列表中出现了两次。删除重复项,您将看到您期望的输出(我认为)。
pattern = ['*.results','*.reports','*.log','*.sum', ... '*.log','*.ext','*.sum', ...]