-
Notifications
You must be signed in to change notification settings - Fork 0
/
合并txt.py
64 lines (46 loc) · 1.45 KB
/
合并txt.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
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
# -*- coding:utf-8*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os
import os.path
import time
time1=time.time()
##########################合并同一个文件夹下多个txt################
def MergeTxt(filepath,outfile):
k = open(filepath+outfile, 'a+')
for parent, dirnames, filenames in os.walk(filepath):
for filepath in filenames:
txtPath = os.path.join(parent, filepath) # txtpath就是所有文件夹的路径
f = open(txtPath)
##########换行写入##################
k.write(f.read()+"\n")
k.close()
print "finished"
if __name__ == '__main__':
filepath="C:/14/"
outfile="result.txt"
MergeTxt(filepath,outfile)
time2 = time.time()
print u'总共耗时:' + str(time2 - time1) + 's'
filepath="C:/15/"
outfile="result.txt"
MergeTxt(filepath,outfile)
time2 = time.time()
print u'总共耗时:' + str(time2 - time1) + 's'
filepath="C:/16/"
outfile="result.txt"
MergeTxt(filepath,outfile)
time2 = time.time()
print u'总共耗时:' + str(time2 - time1) + 's'
filepath="C:/17/"
outfile="result.txt"
MergeTxt(filepath,outfile)
time2 = time.time()
print u'总共耗时:' + str(time2 - time1) + 's'
filepath="C:/18/"
outfile="result.txt"
MergeTxt(filepath,outfile)
time2 = time.time()
print u'总共耗时:' + str(time2 - time1) + 's'
raw_input("Press <enter>")