zoukankan      html  css  js  c++  java
  • 合并目录中的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'
  • 相关阅读:
    用于主题检测的临时日志(594fb726-af0b-400d-b647-8b1d1b477d72
    返璞归真vc++之字符类型
    DIV居中
    程序员职业生涯
    枚举进程句柄
    不使用mutex设计模式解决并发访问cache
    服务器权重分配算法
    xmemecached中的一致性hash算法
    安卓课堂练习
    pythonPTA---分支循环与集合7-1 jmu-python-韩信点兵 (20分) 7-2 打印数字矩形 (10分) 7-3 成绩统计 (10分) 7-4 找列表中最大元素的下标 7-5 删除列表中的重复值
  • 原文地址:https://www.cnblogs.com/shaivas/p/6950118.html
Copyright © 2011-2022 走看看