1

I'm working on searching for strings inside text files. What I have is a CSV file with multiple lines of a single word. Now I need to search files in multiple folders and subfolders for the words in this CSV file. In the end I would like to dump out the results into a text file. The results should have the original word and the result file name that the string was found in. How do you loop through a CSV file with strings while searching files for with these strings? I've only come across individual Python programs that will search for one string in a folder and then print out the results. I've modified one of these to print to a file but am having trouble looping through a CSV search string file.

1 Answer 1

1

I suggest the following approach: read the CSV file and create the list of search words. Then create a regular expression out of them, matching any of these words:

regexp = re.compile( '(' + '|'.join(words) + ')' )

Then go through the files using os.walk and apply the regexp to them using re.search.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.