Intro:
I've written a small piece of Python program which is looking for a given sentence in multiple sub directories of a given path.
I'm looking for improvements regarding the speed of my script.
Code:
from os import walk
from os.path import join
def get_magik_files(base_path):
"""
Yields each path from all the base_path subdirectories
:param base_path: this is the base path from where we'll start looking after .magik files
:return: yield full path of a .magik file
"""
for dirpath, _, filenames in walk(base_path):
for filename in [f for f in filenames if f.endswith(".magik")]:
yield join(dirpath, filename)
def search_sentence_in_file(base_path, sentence):
"""
Prints each file path, line and line content where sentence was found
:param base_path: this is the base path from where we'll start looking after .magik files
:param sentence: the sentence we're looking up for
:return: print the file path, line number and line content where sentence was found
"""
for each_magik_file in get_magik_files(base_path):
with open(each_magik_file) as magik_file:
for line_number, line in enumerate(magik_file):
if sentence in line:
print('[# FILE PATH #] {} ...\n'
'[# LINE NUMBER #] At line {}\n'
'[# LINE CONTENT #] Content: {}'.format(each_magik_file, line_number, line.strip()))
print('---------------------------------------------------------------------------------')
def main():
basepath = r'some_path'
sentence_to_search = 'some sentence'
search_sentence_in_file(basepath, sentence_to_search)
if __name__ == '__main__':
main()
Miscellaneous:
As you may already figured out, the reason for my program being so slow resides in search_sentence_in_file(base_path, sentence)
where I need to open each file, read it line by line and look for a specific sentence.
I know I could use a logging library instead of print
ing the results to see who matched what, but that wouldn't serve the purposes of the program. So I'm not looking for that (I'm building this to have a fast way of looking for certain classes / methods / slots definitions in multiple .magik
files in a fast manner. Opening a log file won't satisfy me).
For whoever is interested in the Magik language, and as a bonus of taking your time to take a look at this question, here's a small introduction to Magik.
To sum up:
- is there any way of improving the speed of my program ?
- do you have other suggestions regarding the way I'm searching a sentence ?
PS: I'm looking for answers which aims a Windows distro.
Any other improvements are welcomed !
find
andgrep
, or only the letter with the-R
flag being used? I hear grep has significant efforts to search efficiently and seems to be capable of what you want, and that is searching for text in a file or multiple files. I tend to get these tools available on all OSes. \$\endgroup\$