I have a list of csv files : 'empty.csv' and 'headerOnly.csv' the first file is empty and the second only has one row (the header)
I'm trying to raise an exception when the file is empty and continue on when it only includes the header.
with open(csvfile, mode='rb') as csv_file:
#check for empty files
print(sum(1 for line in csv_file))#prints number of lines in file
if(sum(1 for line in csv_file) == 0):
print("csv file is empty")
elif(sum(1 for line in csv_file) == 1):
print( "only has header")
I know for a fact that the 'headerOnly.csv' file has ONE line and the first print statement validates that. Even if the print statment for 'headerOnly.csv' prints out 1, it never reaches the elif statement and even prints 'csv file is empty' for that file which is not.. not sure why it isn't reaching the elif