-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcsv_merge.py
More file actions
executable file
·65 lines (48 loc) · 1.96 KB
/
csv_merge.py
File metadata and controls
executable file
·65 lines (48 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
'''
@author: Matthew C. Jones, CPA, CISA, OSCP
IS Audits & Consulting, LLC
TJS Deemer Dana LLP
Merge multiple CSV files into a single output file with only one header row
NOTE - ALL FILES MUST HAVE SAME HEADER / FORMAT TO WORK PROPERLY
See README.md for licensing information and credits
'''
import argparse
import os
import glob
def main():
#------------------------------------------------------------------------------
# Configure Argparse to handle command line arguments
#------------------------------------------------------------------------------
desc = "Merge multiple CSV files into a single output file"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('input_folder', action='store', nargs='?',
help='Directory containing CSV files to merge \n \
(defaults to working directory if none specified)'
)
args = parser.parse_args()
input_folder = args.input_folder
#------------------------------------------------------------------------------
# Main stuff
#------------------------------------------------------------------------------
if not input_folder:
input_folder = os.getcwd()
print 'no directory specified - using working directory:'
print input_folder
print ''
merge_csv(input_folder)
def merge_csv(input_folder):
csv_files = glob.glob(os.path.join(input_folder,"*.csv"))
output_file = os.path.join(input_folder, "merged.csv")
header_saved = False
with open(output_file,'wb') as fout:
for filename in csv_files:
with open(filename) as fin:
header = next(fin)
if not header_saved:
fout.write(header)
header_saved = True
for line in fin:
fout.write(line)
if __name__ == '__main__':
main()