Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 04-search-file-using-extension.py
Original file line number Diff line number Diff line change
@@ -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)