Ask Question

Define a function sum_file that consumes a string representing a filename and returns a number. This number will represent the sum of all the numbers in the given file. Assume that all files will have each number on their own line. Note: Your function will be unit tested against multiple arguments. It is not enough to get the right output for your own test cases, you will need to be able to handle any valid filename. You can safely assume that the file will be a non-empty sequence of numbers, each on their own new line. Note: You cannot simply print out a literal value. You must use a looping pattern to calculate for any file like this. Note: You cannot embed the text of the file directly in your program. Use the appropriate file handling style to access the data in the file.

+2
Answers (1)
  1. 29 November, 02:48
    0
    import os

    def sum_file (file_name):

    def is_string_number (value):

    try:

    float (value)

    return True

    except ValueError:

    return False

    total = 0

    for _, __, files in os. walk ('.'):

    for file in files:

    if file_name = = file:

    if not file. endswith ('txt'):

    raise NotImplementedError ('Handling data of files not text file was not implemented')

    with open (file) as open_file:

    file_contents = open_file. read ()

    file_contents = file_contents. split ()

    for char in file_contents:

    if is_string_number (char):

    total + = float (char)

    return total

    raise FileNotFoundError ('File was not found in the root directory of the script')

    Explanation:

    The above code is written in Python Programming Language. The os module is a Python's module used for computations involving the operating system of the machine the code is running in. It was used in this snippet because we will doing file search.

    We have two functions (sum_file & is_string_number). The main function here is sum_file. The is_string_number function is more of like a util which checks if the content of the file is a number returning a Boolean value (True if value is a number or False otherwise).

    We scan through the root directory of the path where the Python script is. If filename passed is not in the script's root directory, a FileNotFoundError is raised with a descriptive error message.

    If a file is found in the root directory tallying with the argument passed, we check if it is a text file. I am assuming that the file has to be a text file. Python can handle file of other types but this will require other packages and modules which does not come pre-installed with Python. It will have to be downloaded via the Python's pip utility. If the file is not a text file, a NotImplementedError is raised with a descriptive error message.

    However, if the file exists and is a text file, the file is opened and the contents of the file read and splitted by the newline character. Next, we loop through the array and checking if the content is a number by using our is_string_number function. If the function returns true, we convert to float and add to an already declared variable total

    At the end of the loop, the value of total is returned
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Define a function sum_file that consumes a string representing a filename and returns a number. This number will represent the sum of all ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers