question archive This is what final product should show below! ————————————————————————————————————————— Processing file good_data
Subject:Computer SciencePrice:4.86 Bought8
This is what final product should show below!
—————————————————————————————————————————
Processing file good_data.txt
Record count = 6
Total = 281
Average = 46.83
Processing file bad_data.txt
Error! bad_data.txt contains non-numeric data
Processing file empty_file.txt
Error! empty_file.txt is empty. Cannot calculate average
Processing file does_not_exist.txt
Error! does_not_exist.txt File not found
——————————————————————————————————
# This is code I have so far having trouble getting it to print out error if none int in data files. It needs to be all numbers for # it to get average, total and record count. I will send link with files under this.
#. https://we.tl/t-RpShgnzCkr
def main():
process_file("good_data.txt")
process_file("bad_data.txt")
process_file("empty_file.txt")
process_file("does_not_exist.txt")
def process_file(param_str_file_name):
record count = 0
total = 0
average = 0
print("Processing file", param_str_file_name)
print("tError!", param_str_file_name, " is empty. Cannot calculate averagen")
print("tRecord count = ", num_rec)
print("tTotal = ", total)
print("tAverage = " , f"{total/num_rec:.2f}", "n")
print("tError!", param_str_file_name, " File not foundn")
. print("tError!", param_str_file_name, "contains non-numeric datan")
if __name__ == "__main__":
main()

Python Programming:
?NOTE:
Step-by-step explanation
Instruction:
Python Code:
# main function
def main():
# function calls with text files
process_file("good_data.txt")
process_file("bad_data.txt")
process_file("empty_file.txt")
process_file("does_not_exist.txt")
# function to process text file
def process_file(param_str_file_name):
# variables declaration
record_count = 0
total = 0
average = 0
# printing statement
print("Processing file", param_str_file_name)
# try block to read data of the text file
try:
# opening text file read mode
File = open(param_str_file_name, 'r')
# looping over text file line by line
for line in File:
# stripping the line to remove extra spaces at both ends
line = line.strip()
# if the current line is not empty then
if len(line) != 0:
# incrementing record_count by 1
record_count = record_count + 1
# adding current line number to the total
total = total + int(line)
# closing text file
File.close()
# if the record_count is 0 after reading text file
if record_count == 0:
# printing error statement
print("Error!", param_str_file_name, " is empty. Cannot calculate average\n\n")
# otherwise
else:
# printing result: record_count, total and average
print("Record count =", record_count)
print("Total = ", total)
print("Average =", f"{total/record_count:.2f}", "\n\n")
# if file contains anything other than numbers then
except ValueError:
# # printing error message
print("Error!", param_str_file_name, "contains non-numeric data\n\n")
# if text file doesn't exist in the system then
except FileNotFoundError:
# printing error statement
print("Error!",param_str_file_name,"File not found\n\n")
# main function call
if __name__ == "__main__":
main()
Output:
Please see the attached file for the complete solution

