Warm-up 1). Word count Write a function wordcount (fname) which takes in a string fname, opens...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Warm-up 1). Word count Write a function wordcount (fname) which takes in a string fname, opens the file at location fname, and counts the number of words in the file. Assume that words" here refers to any sequence of one or more consecutive non-whitespace characters. For example, if you called the function with the name of a file with the following contents: import math for in range (100): print("No") it should return 7, because there are 7 "words": 1. import 2. math 3. for 4. i 5. in 6. range (100): 7. print ("No") Create a text file in the same folder as your Python file to test your function. To get started, the file noted above is available on Canvas for download (listed as "no.py"; .py files are just text files after all), but you'll want to make at least one other file to test your function properly. Hint: The .split string method may be helpful here: remember that if you give it no arguments then it splits by any amount of whitespace. 2) Handling bad filenames Right now, if you pass in a bogus file name to your wordcount function (that is, the filename you input does not exist within the directory you are running the program from), you'll most likely get an error that looks something like this: FileNotFound Error: [Errno 2] No such file or directory: 'badfile.txt' We could deal with this by using Python's os module to search our current directory and check whether the file the user is asking for actually exists before opening it. But there's an easier way to approach the problem by using exception handling. Warm-up 1). Word count Write a function wordcount (fname) which takes in a string fname, opens the file at location fname, and counts the number of words in the file. Assume that words" here refers to any sequence of one or more consecutive non-whitespace characters. For example, if you called the function with the name of a file with the following contents: import math for in range (100): print("No") it should return 7, because there are 7 "words": 1. import 2. math 3. for 4. i 5. in 6. range (100): 7. print ("No") Create a text file in the same folder as your Python file to test your function. To get started, the file noted above is available on Canvas for download (listed as "no.py"; .py files are just text files after all), but you'll want to make at least one other file to test your function properly. Hint: The .split string method may be helpful here: remember that if you give it no arguments then it splits by any amount of whitespace. 2) Handling bad filenames Right now, if you pass in a bogus file name to your wordcount function (that is, the filename you input does not exist within the directory you are running the program from), you'll most likely get an error that looks something like this: FileNotFound Error: [Errno 2] No such file or directory: 'badfile.txt' We could deal with this by using Python's os module to search our current directory and check whether the file the user is asking for actually exists before opening it. But there's an easier way to approach the problem by using exception handling.
Expert Answer:
Related Book For
Applied Regression Analysis and Other Multivariable Methods
ISBN: 978-1285051086
5th edition
Authors: David G. Kleinbaum, Lawrence L. Kupper, Azhar Nizam, Eli S. Rosenberg
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
A machine fills containers with a particular product. The standard deviation of filling weights computed from past data is 0.6 ounces. If only 2% of the containers hold less than 18 ounces, what is...
-
Refer to the bond details in Problem 10-2B, except assume that the bonds are issued at a price of $4,192,932. Problem 10-2B Romero issues $3,400,000 of 10%, 10-year bonds dated January 1, 2013, that...
-
Give full details of the centralized two-phase commit protocol in a distributed environment. Outline the algorithms for both coordinator and participants.
-
Analyze the patient satisfaction data in Table B. 17 for multicollinearity. Satisfaction Age Severity Surgical-Medical Anxiety 68 55 50 0 2.1 77 46 24 1 2.8 96 30 46 1 3.3 80 35 48 1 4.5 43 59 58 0 2...
-
Kimberly Manis, an architect, organized Manis Architects on January 1, 2016. During the month, Manis Architects completed the following transactions: a. Issued common stock to Kimberly Manis in...
-
Solve 2 (76 +8)
-
Describe the flow of costs for the purchase of a machine. At what point do such costs become expenses? Why is it necessary to record the expense related to the machine in the same period as the...
-
In accordance with the assignment technique, discuss the various control techniques that will be used with this specific design. Develop a hypothetical research scenario that would necessitate the...
-
Develop the appropriate primary research question to be associated with this design. Develop a hypothetical research scenario that would necessitate the use of a Predictive Design. The research will...
-
Lahoma Enterprises, Inc., needs $15 million to finance a major product development. The project will be financed from the following sources: Lahoma Enterprises effective tax rate is 34 percent with...
-
Identify the research scenario, including the relevant independent variable (which would have already occurred) and dependent variable. Develop a hypothetical research scenario that would necessitate...
-
Identify the research scenario including the relevant independent variable and dependent variable. Develop a hypothetical research scenario that would necessitate the use of a Solomon N-Group Design....
-
Consider the following $1,000 par value zero-coupon bonds: Bond Years to Maturity YTM (%) 6.3% A B 7.3 7.8 8.3 OnE: D 1 Bond B C D 2 3 According to the expectations hypothesis, what is the market's...
-
(a) Bright Sdn Bhd (BSB) is a tax resident manufacturing company in Johor, which involves in ceramic tiles. Currently, BSBs annual sales turnover has been forecasted to be around RM 300,000 for the...
-
In Problem 1, separate straight-line regressions of SBP on QUET were compared for smokers (SMK = 1) and nonsmokers (SMK = 0). a. Define a single multiple regression model that uses the data for both...
-
This problem refers to the 1990 Census data presented in Problem 19 of Chapter 5. In addition to median selected monthly ownership costs (OWNCOST), another independent variable studied was the...
-
The accompanying SAS computer output relates to the radial keratotomy data from Problem 12 in Chapter 8. Use this output and, if necessary, the output from Problem 11 in Chapter 9 to answer the...
-
Define a closely held corporation.
-
Explain the role played by a board of directors of a corporation.
-
Explain the steps in forming a limited liability corporation.
Study smarter with the SolutionInn App