question archive This is what final product should show below! ————————————————————————————————————————— Processing file good_data

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()

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Python Programming:

 

?NOTE:

 

  • Please don't mark this answer as unhelpful, unless I declare that I can't help you anymore. If you still need any help with this question, please let me know in the comment section. I will definitely help you. If you don't have any queries then please don't forget to mark it as helpful, It might good for me. Thank you.

Step-by-step explanation

 

 

 

Instruction:

 

  • Keep in mind that all the four files, good_data.txt, bad_data.txt, empty_file.txt and this Python code file must be saved in same folder while executing this program.
  • If you still face any issues with this code, then let me know in the comment section.

 

 

 

 

Python Code:

 

  • I'm using try - except block in the below code to handle file errors.

 

# 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

Related Questions