The coding should be in Python Develop a class Textfile that
The coding should be in Python, Develop a class Textfile that provides methods to analyze a text file. The class Textfile will support a constructor that takes as input a file name (as a string) and instantiates a Textfile object associated with the corresponding text file. The Textfile class should support methods nchars(), nwords(), and nlines() that return the number of characters, words, and lines, respectively, in the associated text file. The class should also support methods read() and readlines() that return the content of the text file as a string or as a list of lines, respectively, just as we would expect for file objects. The class should support method grep() that takes a target string as input and searches for lines in the text file that contain the target string. The method returns the lines in the file containing the target string; in addition, the method should print the line number, where line numbering starts with 0. Finally, Add method words() to class Textfile. It takes no input and returns a list, without duplicates, of words in the file.
Solution
Here is the python program for the above scenario:
class Textfile():
def __init__(self, filename):
self.file = open(filename)
def nchars(self):
return len(self.file.read())
def nwords(self):
content = self.file.read()
words = content.split()
return len(words)
def nlines(self):
content = self.file.read()
return content.count(\'\ \')
