title: Python for loop causes system crash
body: <p>I need to find all lines of a text file containing a particular string and write each line in a different text file. How can I improve my code to prevent system crash for reading first 5,000,000,000 lines of a big text file (6GB size)? After compiling the code my pc running slow, and suddenly freezing up. Even I stop compiling process, memory remains occupied and same problem comes up. My IDE is Spyder and I use Python 2.7. 
  Thank you!</p>
  
  <p>My code is:</p>
  
  <pre><code>import fileinput
  
  ot = 'N'
  j = 1
  i = 1
  string = "ABCD"
  
  for line in fileinput.input(['/../myfile.txt']):
      if i&lt;=5000000000:
          if string in line:
              output = open(ot + str(j) + '.txt', 'w')
              output.write(line)
              output.close()
              j += 1
          i += 1
  </code></pre>