title: How can I split a text file into multiple text files using python?
body: <p>I have a text file that contains the following contents. I want to split this file into multiple files (1.txt, 2.txt, 3.txt...). Each a new output file will be as the following. The code I tried doesn't split the input file properly. How can I split the input file into multiple files?</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>#!/usr/bin/python
  
  with open("input.txt", "r") as f:
      a1=[]
      a2=[]
      a3=[]
      for line in f:
          if not line.strip() or line.startswith('A') or line.startswith('$$'): continue
          row = line.split()
          a1.append(str(row[0]))
          a2.append(float(row[1]))
          a3.append(float(row[2]))
  f = open('1.txt','a')
  f = open('2.txt','a')
  f = open('3.txt','a')
  f.write(str(a1)) 
  f.close()
  </code></pre>
  
  <p><strong>Input file:</strong></p>
  
  <pre><code>A
  x
  k
  ..
  $$
  
  A
  z
  m
  ..
  $$
  
  A
  B
  l
  ..
  $$
  </code></pre>
  
  <p><strong>Desired output 1.txt</strong></p>
  
  <pre><code>A
  x
  k
  ..
  $$
  </code></pre>
  
  <p><strong>Desired output 2.txt</strong></p>
  
  <pre><code>A
  z
  m
  ..
  $$
  </code></pre>
  
  <p><strong>Desired output 3.txt</strong></p>
  
  <pre><code>A
  B
  l
  ..
  $$
  </code></pre>