IT 117: Intermediate Scripting
Homework 6


Due Sunday, March 10th at 11:59 PM

Deliverables

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.

Specification

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

get_environment_variable_value()

get_dirs_from_path()

get_file_count()

get_file_count_for_dir_list()

print_sorted_dictionary(dictionary)

Suggestions

  1. Create the file hw6.py and in it add the headers for all functions.
    The body of each function should be the Python statement pass.
    There must be a blank line between each function.
    Add the test code below.
    There must be a blank line between the functions and the test code.
    Run the script and fix any errors.
  2. Import the os module.
    Remove the pass statement from get_environment_variable_value.
    There is a variable in the os module that allows you to find the value of any shell variable.
    Get the value of the shell variable whose name is given by the parameter and store it in a local variable.
    Add a print statement inside the function to print the value of this variable.
    Add the following to the test code at the bottom of the script
    print(get_environment_variable_value('PATH'))
    Run the script and fix any errors.
  3. Remove two statements in get_environment_variable_value.
    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 name, is contained in this special os variable.
    If it is, return the value, otherwise return an empty string.
    Add the following to the test code at the bottom of the script
    print(get_environment_variable_value('XXXX'))
    Run the script and fix any errors.
  4. Remove the two print statements 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 local variable.
    Add a print statement to the script that prints the value of this local variable.
    Run the script and fix any errors.
  5. Add an assignment statement to get_dirs_from_path that creates a list from the local variable you defined above.
    You will need to use the string split method to do from the variable you created above.
    Change the print statement inside the function so it prints this list.
    Run the script and fix any errors.
  6. Remove the print statements from get_dirs_from_path.
    Add code to return the list.
    Add the following to the test code at the bottom of the script
    print(get_dirs_from_path())
    Run the script and fix any errors.
  7. Remove the print statement you just 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.
    Run the script and fix any errors.
  8. Create the empty dictionary file_counts before of the for loop in get_file_count_for_dir_list.
    Inside the for remove the print statement.
    In it's place write an assignment statement that make an entry in file_counts for each directory.
    The value given to each dictionary entry should be 0.
    Outside the for loop at the end of the function, print file_counts.
    Run the script and fix any errors.
  9. Remove the print statement from get_file_count_for_dir_list.
    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.
    Run the script and fix any errors.
  10. Change the for loop in print_sorted_dictionary so it prints the dictionary sorted by directory.
    Run the script and fix any errors.
  11. Go back to get_file_count_for_dir_list and replace 0 in the assignment statement for file_counts 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 returns this value.
    Run the script and fix any errors.
  12. Before the return statement in get_file_count add a call to the 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.
  13. 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 0.
    Run the script and fix any errors.
  14. There is a function in the os module that returns a list of everything inside a directory.
    Before the return statement in get_file_count, call this os module function and store the list in a local variable.
    Write a print statement to print this new variable.
    Run the script and fix any errors.
  15. Remove the print statement from get_file_count.
    Replace this with an assignment statement that sets the local variable count to 0.
    Now add a loop that iterates through this list and prints each entry in the list.
    Change the return statement to return the value of count.
    Run the script and fix any errors.
  16. Add an import statement to the top of your script for the os.path module.
    There is a function in the os.path module that will tell you whether something is a file or not.
    Inside the loop you wrote above remove the statement that prints each entry.
    Replace it with an if statement that uses this function to tell whether each item in the list is a file.
    The code block of this if statement should print something if the entry is a file.
    Run the script and fix any errors.
  17. Replace the print statement in the if statement with a statement that increments count by 1.
    This should only happen if the entry is a file.
    Run the script and fix any errors.
  18. Make sure you have removed all print statements except the one in print_sorted_dictionary.

Testing

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)
    print_sorted_dictionary(dir_count)
Copy and past this code into the bottom of your script file.

Your output should look something like this
$ ./hw6.py 
/bin 172
/sbin 261
/snap/bin 0
/usr/bin 2001
/usr/games 1
/usr/lib/oracle/12.1/client64/bin 4
/usr/local/bin 5
/usr/local/games 0
/usr/local/sbin 0
/usr/sbin 277