合并目录中的txt文件

txt 文件 合并 并 按章节 分隔

 1 # -*- coding: utf8 -*-
 2 import os
 3 
 4 result_name = 'result.txt'
 5 
 6 def resplit_txt_in_folder(folder_name):
 7     for root,dirs,files in os.walk(folder_name):
 8         for file_ in files:
 9             if -1 != file_.find('.txt'):
10                 resplit_txt_with_name(os.path.dirname(folder_name) + '/' +file_)
11 
12 def resplit_txt_with_name(txt_name):
13     '''
14     resplit the txt file, replace prefix with '章节'
15     '''
16     if True != os.path.exists(txt_name):
17         print 'resplit txt failed: file do not exist:' + txt_name
18         return
19     try:
20         print txt_name
21         handler = open(txt_name,'r')
22         result = open(result_name,'a')
23         buf = handler.readlines()
24         loop = 1
25         for index,line in enumerate(buf):
26             if -1 != line.find('@#'):
27                 #buf[index] = '第' + str(loop) + '节 ' +line.decode('gb2312').encode('utf8') + '
'
28                 buf[index] = '' + str(loop) + '' +line
29                 loop=loop+1
30             else:
31                 #buf[index] = line.decode('gb2312').encode('utf8') + '
'
32                 pass
33         result.writelines(buf)
34         result.close()
35 
36     finally:
37         handler.close()
38         print 'resplit done'
39 
40 def merge_txt_in_folder(folder_name):
41     '''
42     merge all of the text file into one
43     '''
44 
45     r = open(result_name,'a')
46     loop = 1
47 
48     for root,dirs,files in os.walk(folder_name):
49         for file_ in files:
50             if -1 != file_.find('.txt'):
51                 try:
52                     print file_
53                     handler = open(file_,'r')
54                     b = handler.read()
55                     r.write('' + str(loop) + '' + file_.split('.')[0].decode('gb2312').encode('utf8') +'
') 
56                     r.write(b + '
')
57                     loop = loop+1
58                 finally:
59                     handler.close()
60 
61         r.close()
62 
63 if __name__ == '__main__':
64     path_name = raw_input("input the path :")
65     print 'Start'
66     if '' == path_name:
67         #merge_txt_in_folder('.')
68         resplit_txt_in_folder('.')
69     else:
70         if True == os.path.exists(path_name):
71             #merge_txt_in_folder(path_name)
72             resplit_txt_in_folder(path_name)
73         else:
74             print 'path do not exist:',path_name
75     print 'End'
原文地址:https://www.cnblogs.com/shaivas/p/6950118.html