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

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

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