# Append CSV Files Together With Python (Skipping Headers)

Authors Note: This code has been updated and improved upon. I won’t remove this post, but I highly suggest you use the code from the improved code in the post found here.

Last night I had a project where the client needed 30 .csv files compiled into a single sheet. I figured that I could concatenate them together using Python real quick. Python makes tasks like this fairly trivial, but I like to experiment a bit so rather than just write a loop appending each file I wrote a class to do it with some methods. It’s reusable for anyone who has a similar task. I had wanted to add a utility which would compare the headers of 1 file with the headers of the previous file but I was on the clock so I’ll save that bit for another day.

Python has a csv module but I don’t use it in this class yet. The Python csv module is really just a utility in itself to make working with CSV text files easier. Since there is no difference between a regular text file and a csv file and I know the format of the files being fed to the class I simply used Pythons open method to grab a handle to my files. I did import the csv python module though to remind me to use it when I extend this in the future.

Authors Note: This code has been updated and improved upon. I won’t remove this post, but I highly suggest you use the code from the improved code in the post found here.

Last night I had a project where the client needed 30 .csv files compiled into a single sheet. I figured that I could concatenate them together using Python real quick. Python makes tasks like this fairly trivial, but I like to experiment a bit so rather than just write a loop appending each file I wrote a class to do it with some methods. It’s reusable for anyone who has a similar task. I had wanted to add a utility which would compare the headers of 1 file with the headers of the previous file but I was on the clock so I’ll save that bit for another day.

Python has a csv module but I don’t use it in this class yet. The Python csv module is really just a utility in itself to make working with CSV text files easier. Since there is no difference between a regular text file and a csv file and I know the format of the files being fed to the class I simply used Pythons open method to grab a handle to my files. I did import the csv python module though to remind me to use it when I extend this in the future.

## Python class to append csv files together

Here is a link to the Gist on Github.

A few things I would like to do is add header checking which can notify the user if the headers from 1 file don’t match up with the headers from the next. In cases like that it would be good for the user to be able to set options of what the class should do in that case. It could either 1. Terminate, 2. Ignore, 3. Append with headers, 4. Write to another file. With option 4 it could be possible to pass a folder with 100 files into the class and it could append all files with common headers together. Another thought would be to add max file (row) size so the user could handle overflow.