在下面的代码中,程序从用户那里获取字符串数据,并将其转换为ascii和hex,并在某个目录中的所有.log和.txt文件中搜索纯字符串、hex和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}nn"
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}nn"
output = template.format(fileinput.filelineno(), fileinput.filename(), result.group())
print output
temp.write(output)
else:
print "There are no files in the directory!!!"
各位,我认为user706808希望搜索文件中所有出现的搜索字符串,并:
- 对于每次出现,如果在文件中找到字符串,则按行打印行号、文件路径名
- 如果在文件中找不到字符串,则按file打印文件路径名(但不打印内容)和搜索字符串。最简单的方法是保持布尔(或int)对出现次数(nMatches)的跟踪,然后在关闭文件或路径名断章取义之前在末尾打印无匹配消息(如果nMatches为0或False)
你能确认吗?假设这就是你想要的,你所需要改变的就是分割这条巨大的代码线。。。
for line in fileinput.input(walk_dir(directory, (".log", ".txt"))):
进入。。。
for curPathname in walk_dir(directory, (".log", ".txt")):
nOccurrences = 0
for line in fileinput.input(curPathname):
result = regex.search(whitespace.sub('', line))
if result:
...
nOccurrences += 1 # ignores multiple matches on same line
# You don't need an 'elif not result' line, since that should happen on a per-file basis
# Only get here when we reach EOF
if (nOccurrences == 0):
NOW HERE print the "not found" message, for curPathname
# else you could print "found %d occurrences of %s in ..."
听起来不错吗?
顺便说一下,您现在可以简单地将fileinput.filename()称为"curPathname"。
(此外,您可能希望将功能抽象为函数find_occurrences(searchstring,pathname),该函数返回int或布尔值"nOccurrences"。)