txt 文件 合并 并 按章节 分隔

 # -*- coding: utf8 -*-
import os result_name = 'result.txt' def resplit_txt_in_folder(folder_name):
for root,dirs,files in os.walk(folder_name):
for file_ in files:
if -1 != file_.find('.txt'):
resplit_txt_with_name(os.path.dirname(folder_name) + '/' +file_) def resplit_txt_with_name(txt_name):
'''
resplit the txt file, replace prefix with '章节'
'''
if True != os.path.exists(txt_name):
print 'resplit txt failed: file do not exist:' + txt_name
return
try:
print txt_name
handler = open(txt_name,'r')
result = open(result_name,'a')
buf = handler.readlines()
loop = 1
for index,line in enumerate(buf):
if -1 != line.find('@#'):
#buf[index] = '第' + str(loop) + '节 ' +line.decode('gb2312').encode('utf8') + '\r\n'
buf[index] = '第' + str(loop) + '节 ' +line
loop=loop+1
else:
#buf[index] = line.decode('gb2312').encode('utf8') + '\r\n'
pass
result.writelines(buf)
result.close() finally:
handler.close()
print 'resplit done' def merge_txt_in_folder(folder_name):
'''
merge all of the text file into one
''' r = open(result_name,'a')
loop = 1 for root,dirs,files in os.walk(folder_name):
for file_ in files:
if -1 != file_.find('.txt'):
try:
print file_
handler = open(file_,'r')
b = handler.read()
r.write('第' + str(loop) + '章 ' + file_.split('.')[0].decode('gb2312').encode('utf8') +'\r\n')
r.write(b + '\r\n')
loop = loop+1
finally:
handler.close() r.close() if __name__ == '__main__':
path_name = raw_input("input the path :")
print 'Start'
if '' == path_name:
#merge_txt_in_folder('.')
resplit_txt_in_folder('.')
else:
if True == os.path.exists(path_name):
#merge_txt_in_folder(path_name)
resplit_txt_in_folder(path_name)
else:
print 'path do not exist:',path_name
print 'End'

合并目录中的txt文件的更多相关文章

  1. 将指定目录中的txt文件转化成excel文件

    #!/usr/bin/env python#coding:utf-8import reimport osimport globimport xlwtimport sysdir=r"F:\te ...

  2. 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来

    /*4.创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来*/ #import <Foundation/Foundation.h>#defin ...

  3. 在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world

    一.题目 编写一个内核模块,在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world.内核版本要求2.6.18 二.实验环境 物理主机:w ...

  4. Android开发---如何操作资源目录中的资源文件4 ---访问xml的配置资源文件的内容

    Android开发---如何操作资源目录中的资源文件4 XML,位于res/xml/,这些静态的XML文件用于保存程序的数据和结构. XmlPullParser可以用于解释xml文件 效果图: 描述: ...

  5. Android开发---如何操作资源目录中的资源文件3--圆角边框、背景颜色渐变效果、边框颜色

    Android开发---如何操作资源目录中的资源文件3 效果图 1.圆角边框 2.背景颜色渐变效果 1.activity_main.xml 描述: 定义了一个shape资源管理按钮 <?xml ...

  6. Android开发 ---如何操作资源目录中的资源文件2

    Android开发 ---如何操作资源目录中的资源文件2 一.颜色资源管理 效果图: 描述: 1.改变字体的背景颜色 2.改变字体颜色 3.改变按钮颜色 4.图像颜色切换 操作描述: 点击(1)中的颜 ...

  7. python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件

    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...

  8. 在数据库中生成txt文件到网络驱动器中(计算机直接创建的网络驱动器在sql server中没有被找到)

    环境:sql server 2008 一.创建网络驱动器映射 语法:exec master..xp_cmdshell 'net use Z: \\ip地址\网络路径 密码 /user:用户名' 例如: ...

  9. Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

随机推荐

  1. oc27--synthesize,省略getset实现

    // // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { @public int _a ...

  2. 【BZOJ 1598】 牛跑步

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1598 [算法] A*求k短路 [代码] #include<bits/stdc+ ...

  3. PCB SLOT槽孔数量计算方法,同CAM350孔数一致 实现方法

    最近有好几个写脚本的朋友问我,SLOT槽孔孔的如何计算的,要求孔数与CAM350孔数保持一致. 前几年通过在CAM350里面不断测试,结果是:CAM 350中SLOT槽孔,孔与孔之间最高位,凸位高度值 ...

  4. lowbit( )运算

    --------开始-------- lowbit (n) 定义为非负整数n在二进制表示下“最低位的1及其后面所有的0构成的数值. 比如: n = 10 的二进制下为1010,则lowbit (n) ...

  5. c#调用带有自定义表结构的存储过程

    1.新建自定义表结构 CREATE TYPE [dbo].[HBForHBGHDR] AS TABLE( [序号] [int] NULL, [客户编号] [varchar](15) NULL ) GO ...

  6. CAS配置(3)之restful-api接入接口

    第一步,cas服务端对api接口支持 在cas-server-webapp下 pom.xml添加如下依赖 <dependency> <groupId>org.jasig.cas ...

  7. Promise-js异步加载解决方案

    范例: var p = new Promise(function(resolve, reject){ //做一些异步操作 setTimeout(function(){ console.log('执行完 ...

  8. Spring Boot (15) pom.xml设置

    继承spring-boot-parent 要成为一个spring boot项目,首先就必须在pom.xml中继承spring-boot-starter-parent,同时制定其版本 <paren ...

  9. 使用TortoiseSVN碰到的几个问题(2)-冲突解决, 图标重载

    8)解决冲突 冲突分为两种:文件冲突---当两名(或更多)开发人员修改了同一个文件中相邻或相同的行时就会发生文件冲突.下面的属性冲突应该也属于文件冲突. 树冲突---当一名开发人员移动.重命名.删除一 ...

  10. angular js 正序倒叙

      <!DOCTYPE html>   <html lang="en">   <head>   <meta charset="U ...