title: Python 3+, Read In Text File and Write to New File Excluding Range of Lines
body: <p>I'm using Python version 3.6 on a Windows machine. I'm reading in a text file using with <code>open()</code> and <code>readlines()</code>. After reading in the text file lines, I want to write certain lines to a new text file, but exclude certain ranges of lines. I do not know the line numbers of the lines to exclude. The text files are massive and the range of lines to exclude vary among the text files that I'm reading. There are known keywords I can search for to find the start and end of the range to exclude from the text file I want to write to.</p>
  
  <p>I've searched everywhere online but I can't seem to find an elegant solution that works. The following is an example of what I'm trying to achieve.</p>
  
  <pre><code>a  
  b  
  BEGIN  
  c  
  d  
  e  
  END  
  f  
  g  
  h  
  i  
  j  
  BEGIN  
  k  
  l  
  m  
  n  
  o  
  p  
  q  
  END  
  r  
  s  
  t  
  u  
  v  
  BEGIN  
  w  
  x  
  y  
  END  
  z 
  </code></pre>
  
  <p>In summary, I want to read the above into Python. Afterwards, write to a new file but exclude all lines starting at BEGIN and stopping at END keywords.</p>
  
  <p>The new file should contain the following:</p>
  
  <pre><code>a  
  b  
  f  
  g  
  h  
  i  
  j  
  r  
  s  
  t  
  u  
  v  
  z  
  </code></pre>