Write a function called countlines that takes a string

Assignment Help Computer Engineering
Reference no: EM132103985

Problem

In Python, write a function called countLines() that takes a string representing the name of a file as a parameter, and a string representing a word as the second parameter.

The function should go line by line through the file and count the number of lines that contain the word. It should then return that value.

For example, if you are searching through 'example.txt' which contains the following:

Here is a random file...

This is the second line.

There is a blank line above this line.

Then countLines('example.txt', 'is') would return 3.

countLines('example.txt','line') would return 2.

Note that the string 'line' is present once in line 2, and twice in line 4. However, we are only counting the lines that contain this string.

Hint: Look up the function 'find()' in the string class. What does it return if a string is NOT found?

Your program should use exception handling to look for either an IOError or a FileNotFoundError when the opening the input file. If the program can't open the file, print a line that says: 'Unable to open file'. Then end the function using the 'return' statement. (Do not return any value).

Reference no: EM132103985

Questions Cloud

What diameter will you choose for your wire : You're given of copper and asked to make a wire, using all the metal, with a resistance of. Copper has a density of 8900 kg/m3.
How do you find the angle of incidence : How do you find the angle of incidence in this problem, I am having trouble solving this.
Implementation of two algorithms-merge sort and binary : Implementation of any two algorithms from the following algorithms: Merge Sort, Quicksort, Binary Search, Euclidean Algorithm(for Önding gcd).
Determine its minimum point using the matlab routine : Determine its minimum point using the Matlab routine ‘fminsearch'.
Write a function called countlines that takes a string : Write a function called countLines() that takes a string representing the name of a file as a parameter, & a string representing a word as the second parameter.
Find the primes p and q : Find the primes p and q. Find the decoding key, d, using the Euclidean Algorithm. Encode "Eleven" with the public key.
How do you find the average spacing between atoms : How do you find the average spacing between atoms in the crystal for this problem, I am having trouble.
How would you determine an appropriate value for threshold : Your boss asks you to determine the number of erroneous login attempts that should be allowed before a user's account is locked.
Write the five queue operations for this implementation : Write the five queue operations for this implementation.Compare this implementation with the circular queue implementation discussed in class.

Reviews

Write a Review

Computer Engineering Questions & Answers

  Determine the surface temperature of the stove

A room is to be heated by a coal-burning stove, which is a cylindrical cavity with an outer diameter of 32 cm and a height of 70 cm.

  Write a program to convert floating point to nearest integer

Write a program to convert floating point to nearest integer, using file input and output. The output should identify both input value and the rounded value.

  Discuss authentication and information security

Important points in the articles related to authentication and information security

  Explain why modem block ciphers are designed

Explain why modem block ciphers are designed as substitution ciphers instead of transposition ciphers.

  Use the web to research and evaluate security products from

in a two-page apa formatted paper provide analysis answering the followinguse the web to research and evaluate security

  Explain why pci compliance is so important

Explain why PCI Compliance is so important? What are the implications for companies that are are not PCI compliant?

  What type of cable should use to connect the two switches

Marta is a network administrator who needs to improve an additional connection to a room

  Event on ethical issues related to information technology

Locate a news article based on a recent event on ethical issues related to information technology.

  Program to print the input provided by the user

Write down a program whose main function is merely a collection of the variable declarations and the function calls.

  What questions do you still have about the can bus

What questions do you still have about the synchronous peripheral interface?

  You work for mit consulting a leading it project consulting

you work for mit consulting a leading it project consulting company. your expertise is in project management scoping a

  Find a way to simulate different cache architectures

find a way to simulate different cache architectures through premade address accessing files. this task includes

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd