runtime
187 / 500
question 187 Files & Directories · medium

Combine the daily exports

A folder holds one CSV export per day, each starting with the same header line. Finance wants a single file. Write a function that combines them, keeping the header once.

Functioncombine_exports(folder, dest)
Readsevery *.csv file in folder, in sorted name order (other files are ignored)
Writesdest: the first file's header line once, then every data line of every file, each ending with \n; blank lines are dropped; dest is replaced if it exists
Returnsthe number of data lines written
Edge casesa file may hold only its header, and a file's last line may have no newline; with no CSV files at all, dest is created empty and the result is 0
Examples
callcombine_exports('daily', 'all.csv')returns4

Loading your progress…

Combine the daily exports — Python practice · Runtime