-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeFiles.py
30 lines (27 loc) · 852 Bytes
/
mergeFiles.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
28
29
30
import os,glob,sys
inputdir= sys.argv[1]
outputfile= sys.argv[2]
try:
foname=open(outputfile,'w')
for root, dirs, files in os.walk(inputdir):
files.sort()
for file in files:
if glob.fnmatch.fnmatch(file, '*.txt'):
name=file.split('.')
foname.write(name[0])
foname.write(' ')
try:
inputfile=os.path.join(root,file)
finame=open(inputfile,'r',encoding='UTF-8',errors='ignore')
for line in iter(finame):
foname.write(line.rstrip('\n'))
foname.write(' ')
foname.write('\n')
except IOError:
print ('Error while reading from file')
finally:
finame.close()
except IOError:
print ('Error while writing into file')
finally:
foname.close()