title: Python 3 - CSV writer loop won't close after CSV reader reaches end of file
body: <p>I'm trying to create a program that splits large CSV files into smaller ones. I've got the function working great, except it doesn't ever close the last file, which means it never finishes writing to that file. Here's what I've got: </p>
  
  <pre><code>import csv
  
  length of original file = 1000 rows
  length_of_new_file = 100  # rows
  
  
  def file_splitter(file_name, desired_length):
      with open("{}".format(file_name), 'r') as original_file:
          header = original_file.readline()
          file_reader = csv.reader(original_file,dialect='excel')
          file_count = 0
          new_name = 'split_file_test'
          loop = 0
          while file_reader:
              with open("{}{}.csv".format(new_name, file_count), 'w', newline='') as new_file:
                  new_file.write(header)
                  csv_writer = csv.writer(new_file, delimiter=',')
                  for line in file_reader:
                      if loop == (desired_length-1):
                          csv_writer.writerow(line)
                          new_file.close()
                          file_count += 1
                          loop = 0
                          break
                      else:
                          csv_writer.writerow(line)
                          loop += 1
  
  
  test_file = 'zlotsacontacts.csv'
  
  file_splitter(test_file, length_of_new_file)
  </code></pre>
  
  <p>I have tried adding new_file.close(), but no matter where I put it the last file never seems to get closed. I've also tried different logic in the outermost while loop, like:</p>
  
  <pre><code>while file_reader != '':
  </code></pre>
  
  <p>and</p>
  
  <pre><code>while file_reader not None:
  </code></pre>
  
  <p>but from what I can find, the CSV module can't recognize None values. I'm not sure what I can do to close this loop!</p>