title: Text-file class python
body: <p>Here is the problem I am working on: </p>
  
  <p>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 should support methods nchars(), nwords(), and nlines() that return the number of characters, words, and lines, respectively. </p>
  
  <p>Here is my attempt at the problem:</p>
  
  <pre><code>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('\n')
  </code></pre>
  
  <p>All of my methods seem to work. However, when I run two methods consecutively, the text-file is not saved for the second method and I get 0. </p>
  
  <p>For instance,<br>
  Let example.txt = This is a sentence.</p>
  
  <p>When I run the program, I should get this </p>
  
  <pre><code>&gt;&gt;&gt;&gt;x = Textfile('example.txt')
  &gt;&gt;&gt;&gt;x.nchars()
  &gt;&gt;&gt;&gt;19
  &gt;&gt;&gt;&gt;x.nwords()
  &gt;&gt;&gt;&gt;4 
  &gt;&gt;&gt;&gt;x.nlines()
  &gt;&gt;&gt;&gt;1
  </code></pre>
  
  <p>However, I get this</p>
  
  <pre><code>&gt;&gt;&gt;&gt;x = Textfile('example.txt')
  &gt;&gt;&gt;&gt;x.nchars()
  &gt;&gt;&gt;&gt;19
  &gt;&gt;&gt;&gt;x.nwords()
  &gt;&gt;&gt;&gt;0 
  &gt;&gt;&gt;&gt;x.nlines()
  &gt;&gt;&gt;&gt;0
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>&gt;&gt;&gt;&gt;x = Textfile('example.txt')
  &gt;&gt;&gt;&gt;x.nwords()
  &gt;&gt;&gt;&gt;4
  &gt;&gt;&gt;&gt;x.nchars()
  &gt;&gt;&gt;&gt;0 
  &gt;&gt;&gt;&gt;x.nlines()
  &gt;&gt;&gt;&gt;0
  </code></pre>
  
  <p>As you can see, the methods work individually but the text-file is not saved in the next method.</p>
  
  <p>What am I doing wrong?</p>