Skip to content

Commit 75a9fc2

Browse files
committed
add logic for handling empty csv files
1 parent 498d3db commit 75a9fc2

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

python_csv/append_function.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def append_data(file_path, name, email):
1313
next_id = get_length(file_path)
1414
with open('data.csv', 'a', newline='') as csvfile:
1515
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
16-
writer.writeheader()
16+
if next_id == 0:
17+
writer.writeheader()
18+
next_id = 1
1719
writer.writerow({
1820
"id": next_id,
1921
"name": name,

0 commit comments

Comments
 (0)