Intro:
I've written a small piece of Python program which is looking after 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 after a specific sentence.
I know I could use a logging library instead of printing 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 after 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 about the language, and as a bonus of taking your time to look at this question, here's a small introduction of 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 !