Question
Need help with creating a python script! This script will create a dictionary whose keys are all the directories listed in the PATH system variable,
Need help with creating a python script!
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()
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
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.
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 print statement to your test code that calls get_environment_variable_value with the argument 'PATH' and prints the result.
Add an if statement to the code for get_environment_variable_value that 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 print statement to the test code that prints the value of path_dirs.
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.
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.
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.
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.
Change the for loop 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_count to 0 and return this value.
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.
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.
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.
Remove the print statement. Replace it with for loop 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 print statement with an if statement that increases file_count by 1 if the entry is a file.
Make sure you have removed all print statements except the on in print_sorted_dictionary.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started