Question: 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
This script will create a dictionary whose keys are all the directories listed in thePATH 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()
get_environment_variable_value()
- The header for this function must be
get_environment_variable_value(variable_name) - This function must accept a shell variable as its only parameter
- The function must return the value of this shell variable
- If the variable is not defined, the function must return the empty string
get_dirs_from_path()
- The header for this function must be
get_dirs_from_path() - This function must accept no parameters
- This function returns a list of the directories contained in PATH
get_file_count()
- The header for this function must be
get_file_count(dir_path) - This function must accept a directory name as a parameter
- The function must return the number of files in this directory
- Sub-directories must not be included in this count
get_file_count_for_dir_list()
- The header for this function must be
get_file_count_for_dir_list(dir_list) - The function must accept a list of directories as its only parameter
- This function must return a dictionary, where the keys are the directory names, and the values are the number of files in each directory.
print_sorted_dictionary(dictionary)
- The header for this function must be
print_sorted_dictionary(dict) - This function must accept a dictionary as its only parameter
- The function must print out the key and the value for each directory entry
- The key and value must be on a single line of output
- The entries must be printed sorted by their key
Suggestions
- Create headers for all files. The body of each function should be the Python statement
pass - 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
printstatement to your test code that calls get_environment_variable_value with the argument 'PATH' and prints the result. - Add an
ifstatement to the code for get_environment_variable_valuethat tests whether the environment variable exist. If it does, return the value of the variable, otherwise return the empty string. Change the print statement in the test code section so the argument to the call to get_environment_variable_value is 'XXX. - 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.
- Use the split method to create a list from the variable you created above. Print this list.
- Remove the print statements from get_dirs_from_path. Add code to return the list. Add a
printstatement to the test code that prints the value of path_dirs. - Remove the
printstatement 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. - Create an empty dictionary before the print statement in get_file_count_for_dir_list. Before the
printstatement in theforloop add an assignment statement the gives the value 0 to the variablefile_count. - Replace the print statement in the
forloop with a statement that creates an entry in the dictionary using the directory name as the key and file_count as the value. Add aprintstatement after theforloop that prints this dictionary. - Remove the
printstatement 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 aforloop. - Change the
forloop in print_sorted_dictionary so it prints the dictionary sorted by directory. - 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_countto 0 and return this value. - Add a call to an
osmodule 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. - Put the call to the os module function inside a
try/except/elsestatement. Both theexceptandelseclauses of this statement should return the value of file_count. - Add an
importstatement to the top of your script for the os.path module. In theelseclause, use an os module function to set the value of a variable to the list of entries in the current directory. Print this list. - Remove the
printstatement. Replace it withforloop that prints each entry in the directory. - 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.
- Replace the
printstatement with anifstatement that increases file_countby 1 if the entry is a file. - Make sure you have removed all
printstatements 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) print_sorted_dictionary(dir_count) Copy and past this code into the bottom of your script file. Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
