Question: a) Find word in a text file. Implement the function isWord InFile(fileName, word), which given a string fileName and a string word, checks whether
a) Find word in a text file. Implement the function isWord InFile(fileName, word), which given a string fileName and a string word, checks whether or not word is in the file named fileName. (Hint: Read the file in single shot into a strings and feel free to use the the membership test operator in: word in s) Test it on the file named "test.txt" consisting of: This is a test for Problem 3 of Programming Assignment 3 Test program: print (isWordInFile("test.txt", "Programming")) print (isWordInFile("test.txt", "programming")) b) Find word in text file: return line number. This is a variation of Part (a) which requires reading the file line by line. Implement the function wordSearch (fileNane, word), which given a string fileName and a string word (not containing the new line character), searches for word in the file named fileName. If found, it should return the line number of the first occurrence. Otherwise, it should return 0. Feel free to use the membership test operator in for strings. Test it on the file "test.txt" given in Part (a). Test program: print (wordSearch ("test.txt", "Programming")) print (wordSearch ("test.txt","programming")) Output: True False c) Duplicate lines. Implement the function duplicateLines (fileName), which given a string fileName, opens the file name fileName for reading and creates a new file whose content is like fileName but with all lines duplicated. This function assumes that extension of fileName is ".txt", i.e., as a string, fileName is of the form name.txt, for some string name. The new file should be called nameDuplicated.txt. Use assert to stop the program if the extension of fileName is not "txt". Display an appropriate error message. This is a test This is a test for for Output: 3 Test your function on the file "test.txt" given in Part (a). It should create a new file named "test Duplicated.txt" consisting of: Problem 3 of Programming Assignment 3 Problem 3 of Programming Assignment 3 Test it also a file whose extension is not ".txt".
Step by Step Solution
3.32 Rating (146 Votes )
There are 3 Steps involved in it
a Find word in text file def isWordInFilefilesearchstring with openfile ... View full answer
Get step-by-step solutions from verified subject matter experts
