passprint statement to your test code that calls
get_environment_variable_value with the
argument 'PATH' and prints the result.if
statement to the code for get_environment_variable_valuethat tests whether the
environment variable exist.print statement to the test code that prints the
value of path_dirs.print
statement you added to the test code.print statement in the for
loop add an assignment statement the gives the value 0 to the
variable file_count.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.print statement after the for loop
that prints this dictionary.print
statement from get_file_count_for_dir_list and replace it with a
statement that returns the dictionary.for
loop.for
loop in print_sorted_dictionary so
it prints the dictionary sorted by directory.file_count to 0 and return this
value.os module function that will change the current
directory to the dir_path argument to
this function.try/except/else statement.except and else clauses of this
statement should return the value of file_count.import
statement to the top of your script for the os.path module.else clause, use an os module function to set the value of a variable to
the list of entries in the current directory.print
statement.for loop that prints each entry in the
directory.print statement with an if statement that
increases file_countby 1 if the entry is
a file.print statements except the on in print_sorted_dictionary.ScreenShots of code:




dictionary name should not be key as it will override some of the keys, as few directory paths are diffrent but last folder name is same
In the get_file_count_for_dir_list function, I kept both the lines as key for dictionary(key=path key=dictionary name, uncomment this as per your requirement)
SRC CODE FOR EDITING:
##Python 2.7
import os
import collections
def 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
dirInVariable=""
try:
dirInVariable=os.environ['PATH']
except KeyError:
dirInVariable=""
finally:
return dirInVariable
def get_dirs_from_path():
##This function must accept no parameters
##This function returns a list of the directories contained in PATH
strDirNm=get_environment_variable_value('PATH')
if strDirNm=="":
print('System variable is not available')
else:
return list(set(strDirNm.split(';')))
##get_file_count()
##The header for this function must be
def 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
try:
return len([name for name in os.listdir(dir_path) if not os.path.isdir(name)])
except WindowsError:
return 'Path not found'
def 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.
dir_dtl={}
for dir_path in dir_list:
##Please read
##If we keep directory name as key then some of the file count is overwritten, as there are few path where directory name is same
##eg.C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\,C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\ Binn
## dir_dtl[os.path.basename(os.path.normpath(dir_path))]=get_file_count(dir_path)
dir_dtl[dir_path]=get_file_count(dir_path)
return dir_dtl
pass
def 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
od=collections.OrderedDict(sorted(dict.items()))
for keyval in od:
print(keyval,od[keyval])
##Testing Code
##The script must contain the following code to test the functions
if __name__=='__main__':
path_dirs = get_dirs_from_path()
dir_count = get_file_count_for_dir_list(path_dirs)
print_sorted_dictionary(dir_count)
This script will create a dictionary whose keys are all the directories listed in thePATH system...
LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command line arguments and does NOT use the read command. I will give you no credit if your bash script includes a read command. Also, you need to have at least one for..in loop in your code even if there may be other ways to do this problem. The script will go through a list of entries in the current directory (do not process subdirectories):...
PYTHON Hello can someone create the following functions? I'm not sure how to start on them. makeDictionary Define a function named makeDictionary with two parameters. The first argument passed the function must be a list of strings to be used as keys, while the second is a list of the same length containing values (of some type). This function must return a dictionary consisting of the keys paired with the corresponding values. For example, makeDictionary(['apno','value'],['REP-12','450']) must return this dictionary: {'apno':...
the same problem:
We picked the variable name user_dictionary because it will be a dictionary that is created by a user. Other names could be appropriate as well! Though it may seem unnecessary, we'll add a print statement to remind ourself that user_dictionary is empty. Next we'll build up the for loop! Save & Run Load History Show CodeLens 1 # initialize a dictionary 2 user_dictionary - {} 3 print("--- keys in user_dictionary: " + str(list(user_dictionary.keys()) + 5 # write...
Create a Python script file called hw12.py. Add your name at the top as a comment, along with the class name and date. Ex. 1. a. Texting Shortcuts When people are texting, they use shortcuts for faster typing. Consider the following list of shortcuts: For example, the sentence "see you before class" can be written as "c u b4 class". To encode a text using these shortcuts, we need to perform a replace of the text on the left with...
Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based on the number of user enters, to find out how many prime numbers there are up to and including the user inputted number and then display them on the web page. The following are the detailed steps to complete this assignment: Step 1. [30 points] In “find_primeV2.js”, complete isPrime() function by (1) Adding one parameter in function header. That parameter is used to accept...
Write a Perl script that accepts exactly 2 integer arguments
where the first argument must be less than the second argument. The
script will print a comma separated list of integers starting with
the first argument up through the second argument.
The last printed value should be the second command line
argument not be followed by a comma.
The script should also be able to handle the following
errorsituations:
o incorrect number of arguments
o the first...
Functionality to build (4 functions) read_csv_header Define a function named read_csv_header with one parameter. This parameter will be a csv reader object (e.g., the value returned when calling the function csv.reader). The first row read using the parameter is the file's header row. The header row contains the keys for the data stored in the CSV file. This function must return a list containing the strings from that header row. The function parameter will be a csv reader object and...
Write code on best Python style and with appropriate indentation. 1. Given that you have function f that accepts a kwargs argument and does not have a return. You also have a variable d that has been assigned a dictionary. Write a line of code that passes variable d to function f. 2a) Complete the following code to build the states dictionary. The USPresidents.txt file has a number of lines of data, with each line containing two pieces of data:...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...