I have multiple csv files for my application. I am trying to obtain number of rows and columns by reading the csv file. I have two csv files which I send through the argument file_path. First file has 5 rows and 3 columns and Second file has 5 rows and 5 columns. But using the code below I get First file with 4 rows and 3 columns and Second file with 4 rows and 5 columns. I do not understand why it skips reading one line from number of rows.
On the other hand, If I execute code for row_count first and col_count later, it gives StopIteration exception.
It may be a very simple problem but for me being a novice in Python, any help is greatly appreciated. Thanks
def read_text_file(file_path):
with open(file_path, 'r') as f:
reader = csv.reader(f)
col_count = len(next(reader))
print(col_count)
row_count = len(list(csv.reader(f)))
print(row_count)