Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Python program that reads the contents of filename Textfile.txt. The text that is in the file is stored as one sentence per line.

Write a Python program that reads the contents of filename Textfile.txt. The text that is in the file is stored as one sentence per line. The following instruction provides details on how you need to perform the task.

• Ask the user to enter the number of lines he/she wants to view. If the file has fewer lines than the input number, the program should display the entire contents of the file. Otherwise, the program only needs to display the number of lines that the user specified. Ask the user to enter the number of lines he or she wants to view. If the file contains fewer lines than the input number, all of the numbers in the file should be added to the list. Then the program should display the follwing data:

• ▪ Calculate the average number of words per line.

• ▪ The number of uppercase letters.

• ▪ The number of digits in the file.

• ▪ The number of whitespace characters.

• ▪ Handle the following exceptions:

1. It should handle any exceptions that are raised when the specified filename cannot be found or opened.

2. It should handle any exceptions that are raised when a non-integer value is given as a number of lines.

3. It should handle any other exceptions.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Python import sysdef readfilefilename try with openfilename r as file lines filereadlines return lines except FileNotFoundError printfError File filen... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Cost Accounting

Authors: William K. Carter

14th edition

759338094, 978-0759338098

More Books

Students also viewed these Accounting questions

Question

Show that if A is any m n matrix, then Im A = A and AIn = A.

Answered: 1 week ago

Question

What is system performance analysis?

Answered: 1 week ago

Question

What is a transport time?

Answered: 1 week ago