Introduction
Requirement
Prof Jeff Johnson CS 110
Lab 7: Introduction to Recursion (50 points)
Objectives
• Practice writing recursive functions
• Introduction to command-line parameters
• Practice coding file operations
Recursive Directory Traversal
For this lab, you will create a python program (recursive_dir_traversal.py) that will
allow the user to provide the path of a directory to your program as a command line parameter.
Your program must display the total number of python files and the total number of all files in that
directory, its subdirectories, their subdirectories, and so on.
For example, the user may type the following at the terminal:
$> python3 recursive_dir_traversal.py /home/alark/cs110/labs/
Your program must do the following:
• Read in the command line parameters and get the user-provided path.
• Write a recursive function that takes in the path as the input.
• In the recursive function, you have to decide whether the contents of the path provided in the
form. of a string are a file or a directory. For example, if the user provides the path as
/home/alark/cs110/labs/lab1/lab1.py, the provided ‘path’ is actually a file.
Alternatively, /home/alark/cs110/labs/lab1/ is a directory. Use the built-in function
os.path.isfile(path) in your program to identify whether a path is a file or a directory.
• If the path is a file, check if it is a python file. Use a string function for this. Increment the
counter for python files. If the file is not a python file, increment the count for all files but not
the count for python files. Remember, your program should count the total number of files in
the directory and all its subdirectories.
• If the path is a directory, obtain a list of all the files in that directory and call your recursive
function with every element in that list. Given a path, you can obtain the list of all the files in a
directory using files = os.listdir(path).
• At the end of the program, you must display the total number of python files and the total
number of files found.
Submit to your submit directory
Using CyberDuck or your favorite file transfer program, make a directory for lab7 in your submit
directory and upload:
• recursive_dir_traversal.py
• README.txt (a text description of how your program works)