title: How do I split a (.csv) file by the number of rows but keeping the first row (column titles) on each split child file? python
body: <p>I've been trying to create this python csv splitter based on a solution that I found.</p>
  <p>The first issue is keeping the first row (column titles) in each child slipt. It does nott keep the first row as a &quot;header&quot; in each file split.</p>
  <p>The second issue is that I'm trying to send the splitted files to a subdirectory that the script creates. It does finds the directory but it stops at the first split, it does not continue the split loop like before, while I was leaving it on the root folder.</p>
  <p>Does anyone know how this is possible?</p>
  <p>Here's the script:</p>
  <pre><code>import os
  import time
  import pandas as pd
  
  
  # csv file name to be read in
  in_csv = 'D:path\\test.csv'
  
  # get the number of lines of the csv file to be read
  number_lines = sum(1 for row in (open(in_csv)))
  
  # size of rows of data to write to the csv,
  
  # you can change the row size according to your need
  rowsize = 100
  
  
  # start looping through data writing it to a new file for each set
  for i in range(0, number_lines, rowsize):
  
      df = pd.read_csv(in_csv,
                       nrows=rowsize,  # number of rows to read at each loop
                       skiprows=i)  # skip rows that have been read
      # Subdir maker
      directory = 'output'
      parent_dir = 'path\\rows'
  
      path = os.path.join(parent_dir, directory)
      os.mkdir(path)
      print('Directory created.\n')
  
      time.sleep(2)
  
      # csv to write data to a new file with indexed name. input_1.csv etc.
      out_csv = os.path.join(path, 'NewFile' + str(i) + '.csv')
  
      print('Initiating slicer...')
      time.sleep(2)
  
      df.to_csv(out_csv,
                index=False,
                header=True,
                mode='a',  # append data to csv file
                chunksize=rowsize)  # size of data to append for each loop
  
  </code></pre>
  <p>Thank you all in advance.</p>