I am working on a small program for work, and I have looked everywhere for help on this!
What I’m trying to do is to allow the user to put in string to search. The program will search multiple .txt files in a defined directory for the string and then either print the result, or open the .txt file using the default text editor.
Can someone please point me in the right direction for this search feature?
Thanks in advance!
Edit:
This is what I have so far. I cant use grep as this program wil be running on Windows as well as OSX. I have yet to test on Windows, but on OSX my results are access denied.
import os
import subprocess
text = str(raw_input("Enter the text you want to search for: "))
thedir = './f'
for file in os.listdir(thedir):
document = os.path.join(thedir, file)
for line in open(document):
if text in line:
subpocess.call(document, shell=True)
below are hints to your answer 🙂
You can use
os.walkto traverse all the files in the specified directory structure, search the string in the file, usesubprocessmodule to open the file in the required editor…