My program writes data into a CSV file using the pandas' to_csv function. At first run, the CSV file is originally empty and my code wrote data in it (which is supposed to be). At the second run, (take note that I'm still using the same CSV file), my code wrote data in it again (which is good). The problem is, there is a large number of empty rows between the data from the first run and the data from the second run.
Below is my code:
#place into a file
csvFile = open(file, 'a', newline = '',encoding='utf-8')
if file_empty == True:
df.to_csv(csvFile, sep=',', columns=COLS, index=False, mode='ab', encoding='utf-8') #header true
else:
df.to_csv(csvFile, sep=',', columns=COLS, header=False, index=False, mode='ab', encoding='utf-8') #header false
I used the variable file_empty in order for the program to not write column headers if there is already data present in the CSV file.
Below is the sample output from the CSV file:
Last data from first run is in line 396 of CSV file, first row data from second run is in line 1308 of the same CSV file.
So there are empty rows starting from line 397 up to line 1307. How can I remove them so that when the program is run again, there is no empty rows between them?

mode='ab'insideto_csv? Couldn't you just dodf.to_csv(file, columns=COLS, index=False, mode='a')without usingcsvFileand withmode='a'?mode='a'but my data before has alternating empty rows. I searched for a solution and I found out that it was related to being it binary. Anyway, I removed it and changed it back tomode = 'a'. Thanks for pointing that out. But I still have a chunk of empty rows when the program is run again using the same CSV fileopenincorrectly: if you want to useopen(but it's not needed, becauseto_csvalready takes care of opening the file) you should also callcsvFile.close()after each time you write something to it. The new rows added to the file are only visible after closing the file withcsvFile.close().open): dropbox.com/s/5enwrz2ggswns56/Telemedicine_twitter_v3.5.py?dl=0 I addedcsvFile.close()after theto_csvbut I still got the same results. I tried removing theopen(file)for the CSV but I still got the same results.