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")