Reputation: 1014
I am working on a code in python that will look through thousands of text files for certain strings, and then append the names of those text files to one of two lists. I am trying to do it using an if statement with multiple arguments like this:
# up here would be other code
#
with open("/home/textfile.txt", 'r') as f:
textfile = f.read()
if "this phrase" in textfile or "that phrase" in textfile and not "not this phrase" in textfile and not "not that phrase" in textfile:
return True
elif "not this phrase" in textfile or "not that phrase" in textfile:
return False
Now in my code there are many more arguments in these if statements, but for some reason when I get the list of text files that contain "this phrase" or "that phrase" some of them also contain "not this phrase". Why is this happening? Is it because I'm using too many arguments in the if statement? The main goal of the program is to append the text file name to one list or another depending on whether True
or False
is returned to the main function.
Upvotes: 0
Views: 80
Reputation: 824
Agree with Nick's answer. But you said there are many other arguments in the if statements, so you don't want to write all those statements in if loop.
I suggest using two lists. Here is some sample code.
NOTE: Remember that this is a quick and dirty solution. You can improvise this by using generators instead of lists (if your pattern count is high), use lambda functions to reduce the number of lines (though looks complex), etc according to your preferences.
contain_lst = ['pattern1', 'p2', 'p3']
not_contain_lst = ['ncp1', 'ncp2', 'ncp3', 'ncp4']
for each_file in files_list:
with open(each_file) as f:
data = f.read()
contain_match = 1
for each_contain_pattern in contain_lst:
if each_contain_pattern in data:
contain_match = 0
not_contain_match = 0
for each_not_contain_pattern in not_contain_lst:
if each_not_contain_pattern in data:
not_contain_match = 1
if contain_match and not not_contain_match:
print "File %s has all the needed patterns and doesn't have the not needed patterns" % each_file
else:
print "ERROR- File %s doesn't match the requirements" % each_file
Upvotes: 0
Reputation: 2432
You need to group your conditions properly, for example:
if (
("this phrase" in textfile or "that phrase" in textfile) and not (
"not this phrase" in textfile or "not that phrase" in textfile)
):
return True
Upvotes: 2