title: How can I delete "\n" lines from a file in Python?
body: <p>I need to check if the <code>.csv</code> file I'm working with ends with more than 1 <code>"\n"</code> line. If it finds more than a blank line, it removes them all but one.</p>
  
  <p>My code is:</p>
  
  <pre class="lang-py prettyprint-override"><code>import os
  from pathlib import Path
  
  
  def remove_blanks():
      dirname = os.path.dirname(os.path.abspath(__file__))
      path: Path = Path(os.path.join(dirname, "data.csv"))
      with open(path, "r+") as op:
          lines = op.readlines()
          for line in lines:
              if line == "\n":
                  op.write(line.rstrip("\n"))
  </code></pre>
  
  <p>The <code>.csv</code> file is something like <code>['01-01-2019,0,0,0\n', '18-05-2019,33,31,48\n', '\n', '\n', '\n']</code> and the output I'd want is <code>['01-01-2019,0,0,0\n', '18-05-2019,33,31,48\n', '\n']</code> but it doesn't seem to be able to delete any line.</p>