title: How to read and delete first n lines from file in Python - Elegant Solution
body: <p>I have a pretty big file ~ 1MB in size and I would like to be able to read first N lines, save them into a list (<strong>newlist</strong>) for later use, and then delete them.</p>
  
  <p>I am able to do that like this:</p>
  
  <pre><code>import os
  
  n = 3 #the number of line to be read and deleted
  
  with open("bigFile.txt") as f:
      mylist = f.read().splitlines()
  
  newlist = mylist[:n]
  os.remove("bigFile.txt")
  
  thefile = open('bigFile.txt', 'w')
  
  del mylist[:n]
  
  for item in mylist:
    thefile.write("%s\n" % item)
  </code></pre>
  
  <p>I know this doesn't look good in terms of efficiency and this is why I need something better but after searching for different solutions I was stuck with this one.</p>