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. springboot-quartz 实现动态添加,修改,删除,暂停,恢复等功能

    任务相关信息:springboot-quartz普通任务与可传参任务 一.任务实体类 package cloud.app.prod.home.quartz; import java.io.Serial ...

  2. oc34--instancetype和id的区别

    // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int age; ...

  3. Microsoft ASP.NET SignalR

    SignalR类似与JavaScript实时框架,如Socket.IO.SignalR能够完成客户端向服务器的异步通信,并同时支持服务器向浏览器客户端推送事件.SignalR的连接通过日益流行的Web ...

  4. hdu1704——floyd

    Problem Description there are N ACMers in HDU team.ZJPCPC Sunny Cup 2007 is coming, and lcy want to ...

  5. AAC帧格式及编码介绍

    参考资料: AAC以adts格式封装的分析:http://wenku.baidu.com/view/45c755fd910ef12d2af9e74c.html aac编码介绍:http://wenku ...

  6. Java学习笔记之从C++转Java

    之前一直是做C++后台开发的,习惯了命令行和g++,由于工作原因现在开始转java. 1.参考书籍:java编程思想(think in java),java核心技术(core java); 2.怎么在 ...

  7. Python-操作符和表达式

    //: 除后向下取正  -3//2=-2 **: 幂 3**3 = 27 not: ! and: && or: || 除了以上几个之外,其余与C++相同 length = 3 widt ...

  8. Unity5.3.6升级到Unity5.4.4 NGUI出现Ignoring menu item NGUI because it is in no submenu!问题解决方案

    目录Assets/Plugins/NGUI/Scripts/Editor/NGUIMenu.cs文件中找到下图(左)所示,改成(右)图所示

  9. C - Gravity Flip

    Problem description Little Chris is bored during his physics lessons (too easy), so he has built a t ...

  10. 树莓派-基于raspivid实现拍视频

    经过上一篇<<树莓派-安装摄像头模块>>之后 想要用摄像头模块拍一段视频的话,可以从命令行运行 raspivid 工具.下面这句命令会按照默认配置(长度5秒,分辨率1920x1 ...