title: Python Splitting Text file based on a keyword
body: <p>I am trying to write a python program that will constantly read a text file line by line and each time it comes across a line with the word 'SPLIT' it will write the contents to a new text file. </p>
  
  <p>Please could someone point me in the right direction of writing a new text file each time the script comes across the word 'split'. I have no problem reading a text file with Python, I'm unsure how to split on the keyword and create an individual text file each time.</p>
  
  <p>THE SCRIPT BELOW WORKS IN 2.7.13</p>
  
  <pre><code>file_counter = 0
  done = False
  
  with open('test.txt') as input_file:
     # with open("test"+str(file_counter)+".txt", "w") as out_file:
      while not done:
          for line in input_file:
              if "SPLIT" in line:
                  done = True
                  file_counter += 1
              else:
                  print(line)
                  out_file = open("test"+str(file_counter)+".txt", "a")
                  out_file.write(line)
                  #out_file.write(line.strip()+"\n")
                  print file_counter  
  </code></pre>