title: Is it better to open/close a file every time vs keeping it open until the process is finished?
body: <p>I have a text file that is about 50 GB in size, and I am checking the first few characters of each line and writing those to other files specified for that beginning text.</p>
  <p>For example, my input contains:</p>
  <pre><code>cow_ilovecow
  dog_whreismydog
  cat_thatcatshouldgotoreddit
  dog_gotitfromshelter
  ...............
  </code></pre>
  <p>I want to process them in cow, dog, cat, etc. (about 200) categories,</p>
  <pre><code>if writeflag==1:
      writefile1=open(writefile,&quot;a&quot;) #writefile is somedir/dog.txt....
      writefile1.write(remline+&quot;\n&quot;)
      #writefile1.close()
  </code></pre>
  <p>What is the best way, should I close the file? Otherwise, if I keep it open, is <code>writefile1=open(writefile,&quot;a&quot;)</code> doing the right thing?</p>