IT 117: Intermediate Scripting
Homework 6

Due Sunday, March 4th at 11:59 PM


There is one deliverable for this assignment: It must be in an hw6 directory, which you must create inside a hw directory inside you it441 directory.

Make sure the script obeys all the rules in the Script Requirements page.


This script will create a dictionary whose keys are all the directories listed in the PATH system variable, and whose values are the number of files in each of these directories.

The script will also print each directory entry sorted by directory name.

The script will use the following five functions to get the required results







  1. Create headers for all files.
    The body of each function should be the Python statement pass
  2. Import the os module.
    There is a variable in the os module that allows you to find the value of any shell variable.
    Change the code for get_environment_variable_value by creating an assignment statement to set the value of a variable inside the function to the value of the shell variable whose name is given by the parameter to the function.
    Add a print statement inside the function to print the value of this variable.
    Add a print statement to your test code that calls get_environment_variable_value with the argument 'PATH' and prints the result.
  3. Remove the print statement you added above.
    The variable in the os module that contains the shell variable values, is like a dictionary.
    This means that you can use the in operator to see if it has a certain key.
    Write an if/else statement that test whether the shell variable given as a parameter, is contained in this special os variable.
    If it is, return the value, otherwise return an empty string.
    Change the print statement in the test code section so the argument to the call to get_environment_variable_value is 'XXX.
  4. Remove the extra print statement you just added to the test code.
    Change the body of get_dirs_from_path so that it calls get_environment_variable_value with the argument 'PATH' and assigns that value to a variable.
    Print the value of this variable.
  5. Use the split method to create a list from the variable you created above.
    Print this list.
  6. Remove the print statements from get_dirs_from_path. Add code to return the list.
    Add a print statement to the test code that prints the value of path_dirs.
  7. Remove the print statement you added to the test code.
    Change the code in get_file_count_for_dir_list so it contains a for loop that prints every entry in the list it receives as a parameter.
  8. Create an empty dictionary before the print statement in get_file_count_for_dir_list.
    Before the print statement in the for loop add an assignment statement the gives the value 0 to the variable file_count.
  9. Replace the print statement in the for loop with a statement that creates an entry in the dictionary using the directory name as the key and file_count as the value.
    Add a print statement after the for loop that prints this dictionary.
  10. Remove the print statement from get_file_count_for_dir_list and replace it with a statement that returns the dictionary.
    Replace the code in print_sorted_dictionary so it prints out the key-value pairs in the dictionary using a for loop.
  11. Change the for loop in print_sorted_dictionary so it prints the dictionary sorted by directory.
  12. Go back to get_file_count_for_dir_list and replace 0 in the assignment statement for file_count with a call to get_file_count with the directory name as the argument.
    Change the code in get_file_count so it sets the variable file_count to 0 and return this value.
  13. Add a call to an os module function that will change the current directory to the dir_path argument to this function.
    When you test this version of your script, it will fail because there is one directory that you cannot enter.
  14. Put the call to the os module function inside a try/except/else statement.
    Both the except and else clauses of this statement should return the value of file_count.
  15. Add an import statement to the top of your script for the os.path module.
    In the else clause, use an os module function to set the value of a variable to the list of entries in the current directory.
    Print this list.
  16. Remove the print statement.
    Replace it with for loop that prints each entry in the directory.
  17. There is a function in the os.path module that will tell you whether something is a file or not.
    Use this function in the print statement along with the entry name, so say whether the entry is a file.
  18. Replace the print statement with an if statement that increases file_count by 1 if the entry is a file.
  19. Make sure you have removed all print statements except the on in print_sorted_dictionary.

Testing Code

The script must contain the following code to test the functions
    path_dirs = get_dirs_from_path()
    dir_count = get_file_count_for_dir_list(path_dirs)
Copy and past this code into the bottom of your script file.

Your output should look something like this
$ ./ 
/bin: 172
/home/it244gh/bin: 0
/home/it244gh/bin/python/umb: 0
/home/it244gh/bin/shell: 0
/sbin: 261
/snap/bin: 0
/usr/bin: 1996
/usr/games: 1
/usr/lib/oracle/12.1/client64/bin: 4
/usr/local/bin: 4
/usr/local/games: 0
/usr/local/sbin: 0
/usr/sbin: 275