-1

I am currently working on a script to sift through filesystems (both Linux and Windows), to search for specific strings in text files. With what I have below I can select where I want to start, get the full paths to the files with the extensions I want, but the stringstofind does not seem to iterate correctly. I will eventually make this to where I can select the file extensions to search, and enter what strings I am looking for, but for now, I just need this to work so I can understand what I am doing wrong. Thanks in advance!

so far I have:

import os

userinput = input("What directory to search?")
stringstofind = ["String1", "String2", "String3"]
for roots,subdir, files in os.walk(userinput):
   for fname in files:
      #I know this if line is ugly, but like with stringstofind I had a hard 
      #time getting it to iterate through right.
      if ".txt" in fname or ".rtf" in fname or ".doc" in fname:
         filepath = os.path.join(roots,fname)
         with open(filepath, 'r') as f:
            for lines in f:
               if stringstofind in lines:
                  print("found target")
U13-Forward
  • 69,221
  • 14
  • 89
  • 114

2 Answers2

0

I think it's just a bit of a type mismatch:

  • every lines variable produced by for lines in f: loop is actually a string
  • stringstofind is defined as a list

You are trying to do if stringstofind in lines: checking if a list of strings is inside a string.


What you probably meant was to check if any of the strings defined in the stringstofind list is a part of a line. You could use any() for that:

for line in f:
   if any(item in line for item in stringstofind):
      print("found target")
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

instead of

if stringstofind in lines:
    print("found target")

use:

for string in stringstofind:
    if string in lines:
        print("found target")
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19