Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me solve the Python problem Objectives: Create a reusable function Read an external text file from the disk Use try/except to avoid

Can you help me solve the Python problem

Objectives:

Create a reusable function

Read an external text file from the disk

Use try/except to avoid crashing

Write a program that asks the user for a filename, opens the file and reads through the file just once before reporting back to the user the number of characters (including spaces and end of line characters), the number of words, and the number of lines in the file.

If the user enters the name of a file that doesn't exist, your program should give her as many tries as she needs in order to type a valid filename. Obtaining a valid filename from the user is a common operation, so start by writing a separate, reusable function called readValidFilename() that repeatedly asks the user for a filename until she types in the name of a file that your program is able to open. This separate, reusable function will then return a string containing the valid filename as its returned value. Your main program can then open and read the file with that name. You will be able to reuse this function later in the quarter.

Please submit to me THREE things:

your source code file

a recording of the run (which may be at the bottom of your source code file)

the external data file that you used to test your program with.

Hints:

You only need to read through the file one time in order to compile the three statistics required. In order to count the number of words you might find the following string methods helpful: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str (Links to an external site.)Links to an external site. Note that because the methods listed at the above URL are METHODS, you call them with the object at the left side of the "." instead of sending the object in as parameter. For example:

myString.lower()

returns the lower case version of myString.

Here is a test program:

dataFilename = readValidFilename() dataFile = open (dataFilename, "r") for line in dataFile: print (line, end ="") ### OUTPUT ### What is the name of the file you would like to process? x That is not a valid filename, please try again. What is the name of the file you would like to process? data Now is the time for all good humans to come to the aid of their planet

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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_2

Step: 3

blur-text-image_3

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

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago