Skip to content

Instantly share code, notes, and snippets.

@verbalhanglider
Last active April 12, 2018 17:56
Show Gist options
  • Save verbalhanglider/c3778412edc19f02100739e3d8c9fc09 to your computer and use it in GitHub Desktop.
Save verbalhanglider/c3778412edc19f02100739e3d8c9fc09 to your computer and use it in GitHub Desktop.
A basic CLI module using argparse library to find and print out all files in a directory tree
"""a tutorial example of A CLI module using argparse to find all files in a directory tree
"""
from argparse import ArgumentParser
from sys import argv, stderr, stdout
from os import _exit, scandir
from os.path import exists
__VERSION__ = "1.0.0"
def find_all_the_files(path):
"""a recursive function to take a path on a disk and find all the files in a complicated directory tree
:param str path: a directory path that is on disk
:rtype generator
"""
# call a for to iterate through the output of scandir
# this does a flat ls of the directory contents whether
# each content is a file or a directory or a symbolic link
for n_item in scandir(path):
# have to check if the item in hand is a directory
if n_item.is_dir():
# if it is a directory have to call the function
# with the path of the new item
yield from find_all_the_files(n_item.path)
# check if the item is a regular file
elif n_item.is_file():
# if it is a regular file add this to the generator
yield n_item.path
else:
# if the item is neither a directory nor a file then
# something is wierd about this directory
# and you need to know so that you can deal with it
stderr.write("{} cannot be recognized.\n".format(n_item.path))
def main():
"""a main method for the module. takes parameters passed
:rtype int
"""
arguments = ArgumentParser(description="A tool to find all regular files in a complex directory tree",
epilog="Copyright verbalhanglider, version " + __VERSION__)
arguments.add_argument("a_directory", action="store",
type=str, help="An absolute directory path")
parsed_args = arguments.parse_args()
try:
if exists(parsed_args.a_directory):
a_generator = find_all_the_files(parsed_args.a_directory)
for a_file in a_generator:
stdout.write("{}\n".format(a_file))
else:
stderr.write("{} does not exist on this machine!")
# if this completes return proper code to terminal to indicate that program
# completed everything it needed to complete
return 0
except KeyboardInterrupt:
# if user hits Ctrl-C return proper code to terminal to interrupt
return 131
if __name__ == "__main__":
_exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment