Have some lightweight text search engine. It iterates through the .txt files in one folder, searches for selected term and displays the name of the file. All is based on the functionality of os
library:
import os
dirname = '/Users/user/Desktop/test/reports'
save_path = '/Users/user/Desktop/test/folder'
search_terms = ['card']
search_terms = [x.lower() for x in search_terms]
Word = ''.join(search_terms)
for f in os.listdir(dirname):
with open(os.path.join(dirname,f), "r", encoding="latin-1") as infile:
text = infile.read()
if all(term in text for term in search_terms):
print (f)
os.path.join(save_path,f)
3001017.txt
3003402.txt
3002866.txt
3003763.txt
3004961.txt
3003834.txt
3001986.txt
The search is working good itself but I want as a final operation save the .txt files that were received in the result in another folder save_path
For that I trying to use os.path.join(save_path,f)
but it seems that it is not working. What will be the right approach to do that?