Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I create this Python script? This script will create a dictionary whose k

ID: 3838415 • Letter: H

Question

How do I create this 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

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

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

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

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

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

Copy and past this code into the bottom of your script file.

Explanation / Answer

If you keep dictionary name as key, then some of the keys overrridden, as few directory paths are different 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)

##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 ServerXToolsBinn,C:Program Files (x86)Microsoft SQL ServerPDTSBinn 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)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote