From 3afe54f0c7063f408eaec68b539c261d5249bf64 Mon Sep 17 00:00:00 2001 From: Manzoor Ahamed Date: Tue, 6 Jul 2021 17:00:49 +0530 Subject: [PATCH 1/2] Adding the Day4 assignment file to remote --- 04-search-file-using-extension.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 04-search-file-using-extension.py diff --git a/04-search-file-using-extension.py b/04-search-file-using-extension.py new file mode 100755 index 0000000..4ef5cd2 --- /dev/null +++ b/04-search-file-using-extension.py @@ -0,0 +1,29 @@ +#!/opt/anaconda3/bin/python3 + +## Modules +import pathlib +import argparse + +def search_file_using_extension(path,ext): + for file in path.iterdir(): + if file.name.endswith(ext): + print("The file %s ends with the %s extension"%(file,ext)) + +def path_exist(path,ext): + path = pathlib.Path(path) + if path.exists(): + search_file_using_extension(path,ext) + else: + print("The path doesnt exist") + +## Create the parser +parser = argparse.ArgumentParser() + +## Add the arguments +parser.add_argument("--path", help="Directory Path to search for files") +parser.add_argument("--ext", help="mention the extension to search for files") + +## Execute the parse_arg() method +args = parser.parse_args() + +path_exist(args.path,args.ext) From ac0e2022c4dd73c54d7edd49c3af4e176ddba592 Mon Sep 17 00:00:00 2001 From: Manzoor Ahamed Date: Wed, 7 Jul 2021 19:27:06 +0530 Subject: [PATCH 2/2] Modified the code to fetch the size of the file as well --- 04-search-file-using-extension.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/04-search-file-using-extension.py b/04-search-file-using-extension.py index 4ef5cd2..23248bb 100755 --- a/04-search-file-using-extension.py +++ b/04-search-file-using-extension.py @@ -7,7 +7,7 @@ def search_file_using_extension(path,ext): for file in path.iterdir(): if file.name.endswith(ext): - print("The file %s ends with the %s extension"%(file,ext)) + print("The file %s ends with the %s extension and its size is %d in bytes "%(file,ext,file.lstat().st_size)) def path_exist(path,ext): path = pathlib.Path(path)