forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.py
27 lines (22 loc) · 741 Bytes
/
merge.py
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
# author:[email protected]
#!/usr/bin/env python
# -*- coding=utf-8 -*-
import os
# define the result filename
resultfile = 'result.csv'
# the merge func
def merge():
"""merge csv files to one file"""
# use list save the csv files
csvfiles = [f for f in os.listdir('.') if f != resultfile and f.split('.')[1]=='csv']
# open file to write
with open(resultfile,'w') as writefile:
for csvfile in csvfiles:
with open(csvfile) as readfile:
print('File {} readed.'.format(csvfile))
# do the read and write
writefile.write(readfile.read()+'\n')
print('\nFile {} wrote.'.format(resultfile))
# the main program
if __name__ == '__main__':
merge()