title: Pythonic way to write a large number of lines to a file
body: <p>I need to auto-generate a somewhat large Makefile using a Python script. The number of lines is expected to be relatively large. The routine for writing to the file is composed of nested loops, whole bunch of conditions etc.</p>
  
  <p>My options:</p>
  
  <ol>
  <li><p>Start with an empty string and keep appending the lines to it and finally write the huge string to the file using file.write (pro: only single write operation, con: huge string will take up memory) </p></li>
  <li><p>Start with an empty list and keep appending the lines to it and finally use file.writelines (pro: single write operation (?), con: huge list takes up memory)</p></li>
  <li><p>Write each line to the file as it is constructed (pro: no large memory consumed, con: huge number of write operations)</p></li>
  </ol>
  
  <p>What is the idiomatic/recommended way of writing large number of lines to a file?</p>