The elif stament should print the log files and path that were not found in a search that I conduct. However, they yield every line that is searched in a single file (a plethora of info). What am I doing wrong?
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!!!"
Actual Code:
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 result not in line:
output = fileinput.filename()
print output
temp.write(output)
break
else:
print "There are no files in the directory!!!"
You’re iterating over every line of every file passed to
fileinput.input(...), right? And you perform theifstatement for every line. If the condition is true, then youbreak, but if the condition is false, you don’t break, but write totemp. So for every line infileinput.inputthat doesn’t match the condition, you write a line totempand printoutput. (Actually, the above is wrong — see edit below.)Also,
elif str(result) not in line:will have strange results — just useelseas others have suggested. Ifresultevaluates to false in this situation, thenresult == None, which means thatstr(result) == 'None', which means that if a line containsNone, then you’ll have unexpected results.Edit: Ok, actually, looking more closely at your actual code the above is wrong, strictly speaking. But the point remains —
fileinput.input()returns aFileInputobject that in essence concatenates the files and iterates over every line in turn. Since in some cases you don’t want to perform an action per line, but per file, you’ll have to iterate over them individually. You could do this withoutfileinputbut since that’s what you’re using, we’ll stick with that:The way this works: for every file in the list, this prints the first match in the file, or prints the filename if no match was found. You can use
elsewith aforloop in python; theelseblock at the end of the loop is executed if the loop is not broken. Since no match was found, the filename is printed.If you wanted to print out all matches in a file, you could save the matches in a list, and instead of using
else, you could test the list. Simplified example: