В приведенном ниже коде программа получает строковые данные от пользователя и преобразует их в ascii и hex и ищет во всех файлах .log и .txt в определенном каталоге строку в виде простых строк, шестнадцатеричных и ascii значений.Программа печатает строку #, найденный тип строки и путь к файлу, если строка найдена.Однако, я не только хочу, чтобы он печатал файлы, если строка найдена, я также хотел бы, чтобы она печатала файл, путь и строку, которые искали в файлах, которые были найдены, но не найдены.Я новичок, поэтому, пожалуйста, не расстраивайтесь из-за простоты проблемы.Я еще учусь.Благодарю.Код ниже:
elif searchType =='2':
print "\nDirectory to be searched: " + directory
print "\nFile result2.log will be created in: c:\Temp_log_files."
paths = "c:\\Temp_log_files\\result2.log"
temp = file(paths, "w")
userstring = raw_input("Enter a string name to search: ")
userStrHEX = userstring.encode('hex')
userStrASCII = ''.join(str(ord(char)) for char in userstring)
regex = re.compile(r"(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userStrHEX ), re.escape( userStrASCII )))
goby = raw_input("Press Enter to begin search (search ignores whitespace)!\n")
def walk_dir(directory, extensions=""):
for path, dirs, files in os.walk(directory):
for name in files:
if name.endswith(extensions):
yield os.path.join(path, name)
whitespace = re.compile(r'\s+')
for line in fileinput.input(walk_dir(directory, (".log", ".txt"))):
result = regex.search(whitespace.sub('', line))
if result:
template = "\nLine: {0}\nFile: {1}\nString Type: {2}\n\n"
output = template.format(fileinput.filelineno(), fileinput.filename(), result.group())
print output
temp.write(output)
break
elif not result:
template = "\nLine: {0}\nString not found in File: {1}\nString Type: {2}\n\n"
output = template.format(fileinput.filelineno(), fileinput.filename(), result.group())
print output
temp.write(output)
else:
print "There are no files in the directory!!!"