From f486d00013ba948ea62a6d66593b8ec16b40cd54 Mon Sep 17 00:00:00 2001 From: Hirusha Adikari <36286877+hirusha-adi@users.noreply.github.com> Date: Sat, 20 Jan 2024 10:53:21 +0530 Subject: [PATCH] use argparse to get word --- search.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/search.py b/search.py index bdbc505..fa1e586 100644 --- a/search.py +++ b/search.py @@ -1,8 +1,9 @@ import os +import argparse from docx import Document from concurrent.futures import ThreadPoolExecutor -def search(fpath, target): +def check(fpath, target): try: doc = Document(fpath) for paragraph in doc.paragraphs: @@ -10,23 +11,33 @@ def search(fpath, target): return True return False except Exception as e: - print(e) + print(f"Error processing {fpath}: {e}") return False def process_file(file): fname, target = file fpath = os.path.join(os.getcwd(), fname) - if search(fpath=fpath, target=target): - print("Found in ", fname) + if check(fpath, target): + print(f"'{target}' found in {fname}") else: - print("Not found in ", fname) - + print(f"'{target}' not found in {fname}") + def main(): - target = "hirusha" - all_files = [(fname, target) for fname in os.listdir() if fname.endswith(".docx")] - - with ThreadPoolExecutor() as executor: - executor.map(process_file, all_files) + parser = argparse.ArgumentParser(description='Search for a word in .docx files in the current directory.') + parser.add_argument('word', type=str, help='The word to search for') + args = parser.parse_args() + + target = args.word + fall = [(fname, target) for fname in os.listdir() if fname.endswith(".docx")] + + with ThreadPoolExecutor() as executor: + executor.map(process_file, fall) if __name__ == "__main__": - main() + import time + start_time = time.time() + main() + end_time = time.time() + execution_time = end_time - start_time + print(f"Execution Time: {execution_time} seconds") + \ No newline at end of file