Python - Counting blank lines in a text file
<p>Let's say I have a file with the following content(every even line is blank):<br></p>

<blockquote>
  <p>Line 1 <br><br> Line 2 <br><br> Line 3 <br><br> ...</p>
</blockquote>

<p>I tried to read the file in 2 ways:<br></p>

<pre><code>count = 0
for line in open("myfile.txt"):
   if line == '':                   #or if len(line) == 0
     count += 1
</code></pre>

<p>and<br></p>

<pre><code>count = 0 
file = open('myfile.txt')
lines = file.readlines()
for line in lines:
   if line == '':                   #or if len(line) == 0
     count += 1
</code></pre>

<p>But <code>count</code> always remains 0. How can I count the number of blank lines?</p>

<p>Every line ends with a newline character <code>'\n'</code>. Note that it is only one character.</p>

<p>An easy workaround is to check wether the line equals <code>'\n'</code>, or wether its length is <strong>1</strong>, not 0.</p>

python file lines