title: Cannot create more than 2 txt.files in Python
body: <p>Trying to clean up a txt file from a USB GPS and having trouble with 3rd <code>for loop</code>. Currently have...</p>
  
  <pre><code>deletes = ['$GPGGA', '$GPGSA', '$GPGSV', '$PSRF156', ]
  searchquery = '$GPRMC'
  
  with open('Drive_home.txt_rf') as f1:
      with open('Drive_home_1.txt', 'w+') as f2:
          for lines in f1:
              if not any(delete in lines for delete in deletes):
                  f2.write(lines)
  
  with open('Drive_home_1.txt') as f2:
      with open('Drive_home_2.txt', 'w+') as f3:
              lines = f2.readlines()
              for i, line in enumerate(lines2):
                  if line.startswith(searchquery):
                      f3.write(line)
                      f3.write(lines[i + 1])
  
  with open('Drive_home_2.txt') as f3:
      with open('Drive_home_3.txt', 'w+') as f4:
          for lines in f3:
              if len(lines.rstrip('\n')) &gt;= 105:
                  f4.write(lines)
  </code></pre>
  
  <p>For some reason, the script is not creating the 'Drive_home_3.txt.' so not sure why its not.</p>
  
  <p>Also, is it possible just to create one .txt with all <code>for loop</code>?</p>
  
  <p>Update: Currently, Drive_home_1.txt and Drive_home_2.txt will be created to the desktop but Drive_home_3.txt is not. I have tried a quick search of that file but nothing comes up so I was assuming it was just not being created.</p>
  
  <p>This is the exact code that I'm using. I just noticed that <code>lines2</code> should just be <code>lines</code>. I also get the error...</p>
  
  <p>Traceback (most recent call last):
    File "C:\Users****\Desktop\TestV6.py", line 16, in 
      f3.write(lines[i + 1])
  IndexError: list index out of range</p>
  
  <p>I assume that error is just from the data collection at the end of my file which looks like...</p>
  
  <pre><code>$GPRMC  211536  A   3512.0329   N   8507.6987   W   0.24    220.94  300818  A*70                                    
      96.5    50  2   3   -3  236 0   1   1   30-08-18    17:15:36:551                                
  $GPRMC  211537  A   3512.0329   N   8507.6987   W   0   220.94  300818  A*77                                    
      96.5    49  2   3   -4  236 0   1   1   30-08-18    17:15:37:549                                
  $GPRMC  211538  A   3512.0325   N   8507.6985   W   0.31    220.94  300818  A*74                                    
      96.5    49  2   3   -2  236 0   1   1   30-08-18    17:15:38:469                                
  $GPRMC  211539  A   3512.0325   N   8507.6985   W   0   220.94  300818  A*77                                    
  $GPRMC  211540  A   3512.0324   N   8507.6986   W   0.07    220.94  300818  A*7C                                    
  $GPRMC  211540  A   3512.0324   N   8507.6986   W   0.07    220.94  300818  A*7C                                    
  $GPRMC  211541  A   3512.0324   N   8507.6986   W   0   220.94  300818  A*7A                                    
  $GPRMC  211541  A   3512.0324   N   8507.6986   W   0   220.94  300818  A*7A    
  </code></pre>
  
  <p>Update 2:</p>
  
  <p>Cleaned up the script and now I have...</p>
  
  <pre><code>deletes = ['$GPGGA', '$GPGSA', '$GPGSV', '$PSRF156', ]
  searchquery = '$GPRMC'
  
  with open('Drive_home.txt_rf') as f1:
      with open('Drive_home_1.txt', 'w+') as f2:
          lines = f1.readlines()
          for i, line in enumerate(lines):
              if line.startswith(searchquery):
                  if not any(delete in lines[i + 1] for delete in deletes):
                      f2.write(line)
                      f2.write(lines[i + 1])
  </code></pre>
  
  <p>Which outputs one txt. and looks like...</p>
  
  <pre><code>$GPRMC  204249  A   3504.5449   N   8509.0603   W   0.31    352.89  300818  A*7F                                    
      96.5    39  5   14  -2  200 0   1   0   30-08-18    16:42:46:365                                
  $GPRMC  204250  A   3504.5449   N   8509.0605   W   0.32    352.89  300818  A*72                                    
      96.5    39  7   22  -3  168 0   0   0   30-08-18    16:42:47:364                                
  $GPRMC  204251  A   3504.5448   N   8509.0606   W   0.77    352.89  300818  A*70                                    
      96.5    32  9   24  -2  184 0   1   0   30-08-18    16:42:48:362                                
  $GPRMC  204252  A   3504.5448   N   8509.0608   W   0.37    352.89  300818  A*79                                    
      96.5    43  9   15  -4  236 0   1   0   30-08-18    16:42:49:454                                
  $GPRMC  204253  A   3504.545    N   8509.0609   W   0.14    352.89  300818  A*71                                    
      96.5    37  23  17  -4  184 0   1   0   30-08-18    16:42:50:967                                
  $GPRMC  204254  A   3504.545    N   8509.061    W   1.52    50.91   300818  A*45                                    
      96.5    38  10  21  -3  184 0   1   0   30-08-18    16:42:51:451    
  </code></pre>
  
  <p>Now I have a question again, is there away to join the line after "$GPRMC with next line to look like below?</p>
  
  <pre><code>$GPRMC  204244  A   3504.5446   N   8509.0599   W   0   352.89  300818  A*7F 96.5   40  6   11  1   236 0   1   0   30-08-18    16:42:41:451                                                                        
  $GPRMC  204245  A   3504.5447   N   8509.0598   W   0.27    352.89  300818  A*7B 96.5   44  6   9   -9  236 0   1   1   30-08-18    16:42:42:434                                
  $GPRMC  204246  A   3504.5447   N   8509.0598   W   0   352.89  300818  A*7D 96.5   44  7   14  1   217 0   1   0   30-08-18    16:42:43:401                                
  $GPRMC  204247  A   3504.5448   N   8509.0599   W   0.25    352.89  300818  A*75 96.5   35  14  21  2   151 0   1   0   30-08-18    16:42:44:400                
  </code></pre>
  
  <p>I'm not sure how <code>.join()</code> works or if there is a way to concatenate every other line or with previous line <code>if line.startwith</code>. I'm not sure if that every other line will be an issue joining since it has integers and floats in it.</p>