Removal of Headers of CSV file from the Current Working Directory

Python program for removing headers from csv files

import csv,os

os.makedirs(‘headerRemoved’, exist_ok=True)

#loop through every file in the current working directory

for fileName in os.listdir(‘.’):

  if not fileName.endswith(‘.csv’):

    continue

  print(‘Removing Header From CSV’+fileName)

   

  #read the csv file skipping the first row

  csvRows=[]

  csvFileObj=open(fileName)

  readObj=csv.reader(csvFileObj)

  for row in readObj:

    if readObj.line_num ==1:

      continue

    csvRows.append(row)

  csvFileObj.close()

   

  #write csv file

  csvWrite = open(os.path.join(‘HeaderRemoved’,fileName),’w’,newline=” )

  csvWriter = csv.writer(csvWrite)

  for rows in csvRows:

    csvWriter.writerow(rows)

  csvWrite.close()

   

     

       

   

   

Leave a comment

Your email address will not be published. Required fields are marked *