diff --git a/04-search-file-using-extension.py b/04-search-file-using-extension.py new file mode 100755 index 0000000..23248bb --- /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 and its size is %d in bytes "%(file,ext,file.lstat().st_size)) + +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)