0

I'm trying to create a python script able to search with one or more parameters in a file lines but after many test I'm not able to do this This is my python code:

for log_files in os.listdir(path): 
files = os.path.join(path, log_files)
strng = open(files)
for lines in strng.readlines():
            lines = re.sub('"','', lines)
            if (sys.argv[2] and sys.argv[3]) in lines:
                # if sys.argv[3] in lines:
                # print("Matched !!")
                lines=log_files+"__"+lines
                print(lines)
                content_array.append(lines)

The code shows me a result which means that it did not take into consideration the two parameters in the if statement

J.Ghassen
  • 101
  • 1
  • 1
  • 12

1 Answers1

0

The problem with your code is in if condition

 if (sys.argv[2] and sys.argv[3]) in lines:

It should be

if sys.argv[2] in lines and sys.argv[3] in lines:

See this question for reference

Other possible solution is to use all().

arg_list = [sys.argv[2], sys.argv[3]]
for lines in strng.readlines():
    if all(arg in lines for arg in arg_list):
        print(lines)