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.
Function
combine_exports(folder, dest)Readsevery
*.csv file in folder, in sorted name order (other files are ignored)Writes
dest: 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 existsReturnsthe 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 0Examples
callcombine_exports('daily', 'all.csv')returns4