Question
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...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started