title: Is it pythonic to use generators to write header and body of a file?
body: <p>If I was to write a file with this content:</p>
  
  <pre><code>#You have been defeated!
  #It's merely a flesh wound!
  We are the knights who say Ni!
  We are the knights who say Ni!
  We are the knights who say Ni!
  </code></pre>
  
  <p>Would it then be very non-pythonic to do it with a generator using send? I have never seen generators used like this elsewhere.</p>
  
  <pre><code>def write(file, header):
  
      with open(file,'w') as f:
          f.write(header)
          line = (yield)
          while True:
              f.write(line)
              line = (yield)
  
      return
  
  file='holygrail.txt'
  header="#You have been defeated!\n#It's merely a flesh wound!\n"
  generator = write(file,header)
  generator.send(None)
  for i in range(3):
      generator.send('We are the knights who say Ni!\n')
  generator.close()
  </code></pre>
  
  <p>I am asking, because the method above would be hugely beneficial to me instead of opening multiple different file streams in a contextlib stack. I would not have to use the contextlib module at all, if I write my files like this.</p>
  
  <p>I have never asked a question like this before, and I don't know, whether it belongs on stackoverflow or not.</p>