python - how can i search a text file of list of words from user input and print the line which contains these words? -
my query here want search words text file , print lines contain words. words given user input. somehow reached till point it's giving output nothing.
def sip(x): print("====welcome sip log debugger ==== ") file= input("please enter log file path: ") search = input("enter errors want search for(seperated commas): ") search = [word.strip() word in search.lower().split(",")] open(file,'r') f: lines = f.readlines() line = f.readline() word in line.lower().split(): if word in line: print(line), if word == none: print('')
you're reading lines , saving them variable:
lines = f.readlines()
then try read 1 more line:
line = f.readline()
but you've read through whole file, there's nothing read anymore, f.readline()
returns ''
. next try loop through each word in line
variable, ''
.
instead of this, should loop through lines using for line in f:
, like:
with open(file, 'r') f: line in f: line = line.lower() word in search: if word in line: print(line)
i'm not sure you're trying if word == none:
, word can never none
since line
string , word
part of string (you used line.split()
).
Comments
Post a Comment