Files and Exceptions 3 (VS Code Python) Assignment Objectives you must include the following techniques: Your program MUST USE the main function, which must be called correctly (use the template). Additionally, you must include docstrings as shown in the template. File Input File Output Opening Files Closing Files Using Loops to Process Files Processing Records Exceptions 3. Exception Handling Exceptions offer a gracious way to exit a program when things go wrong. Modify the program you wrote for exercise 2 to handle the exceptions (See 6.4 Exceptions on page 341). Test the program using the following text files: text file named 6-3-lines1.txt containing 22 1 8923 9054 9204 9 71 200254 57 text file named 6-3-lines2.txt containing 22 1 8923 9054 9204 9 71 corn! 57 Your program must: Open file designated by user input for reading Include a try block in the main function that holds most of the code from exercise 2. The try block must handle any: IOError exceptions are raised when an attempt is made to open the file. ValueError exceptions are raised when the items read from the file cannot be converted to type float. Use a loop to read all lines from the file Count the number of lines in the file Keep a running total of all the numbers in the file Display the total, the number of lines, and the average of all numbers. All numeric displays must have two decimal points, and the decimal points should align in the column Close the file For the file 6-3-lines1.txt, the output should look like the following: Enter the name of the file: 6-3-lines1.txt Total: 227,595.00 # of Lines: 9.00 Average: 25,288.33 For the file 6-3-lines2.txt, the output should look like the following: Enter the name of the file: 6-3-lines2.txt Non-numeric data is found in the file. If you try to load a file name that does not exist, the output should look like the following: Enter the name of the file: wrong.txt An error occurred while trying to read the file. Try loading the same files in the code from exercise #2, recreate the same mistakes, and compare how exception handling makes for a more user-friendly experience.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Files and Exceptions 3 (VS Code Python)

Assignment Objectives

you must include the following techniques:

  • Your program MUST USE the main function, which must be called correctly (use the template). Additionally, you must include docstrings as shown in the template.
  • File Input
  • File Output
  • Opening Files
  • Closing Files
  • Using Loops to Process Files
  • Processing Records
  • Exceptions

3. Exception Handling

Exceptions offer a gracious way to exit a program when things go wrong. Modify the program you wrote for exercise 2 to handle the exceptions (See 6.4 Exceptions on page 341).

Test the program using the following text files:

text file named 6-3-lines1.txt containing

22
1
8923
9054
9204
9
71
200254
57

text file named 6-3-lines2.txt containing

22
1
8923
9054
9204
9
71
corn!
57

Your program must:

  • Open file designated by user input for reading
  • Include a try block in the main function that holds most of the code from exercise 2.
  • The try block must handle any:
    • IOError exceptions are raised when an attempt is made to open the file.
    • ValueError exceptions are raised when the items read from the file cannot be converted to type float.
  • Use a loop to read all lines from the file
  • Count the number of lines in the file
  • Keep a running total of all the numbers in the file
  • Display the total, the number of lines, and the average of all numbers.
  • All numeric displays must have two decimal points, and the decimal points should align in the column
  • Close the file

For the file 6-3-lines1.txt, the output should look like the following:

Enter the name of the file: 6-3-lines1.txt

Total: 227,595.00
# of Lines: 9.00
Average: 25,288.33

For the file 6-3-lines2.txt, the output should look like the following:

Enter the name of the file: 6-3-lines2.txt

Non-numeric data is found in the file.

If you try to load a file name that does not exist, the output should look like the following:

Enter the name of the file: wrong.txt

An error occurred while trying to read the file.

Try loading the same files in the code from exercise #2, recreate the same mistakes, and compare how exception handling makes for a more user-friendly experience.

 
DAWNA
2
8910mm3u
10
11
12
13
14
|| || ||
Programming Exercise 6-3
# Exception Handing
|| || ||
def main():
# Call the main function.
if
___main__':
_name_
main ()
Transcribed Image Text:DAWNA 2 8910mm3u 10 11 12 13 14 || || || Programming Exercise 6-3 # Exception Handing || || || def main(): # Call the main function. if ___main__': _name_ main ()
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Exception Handling Keywords
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education