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. What is the difference between task and thread?

    http://stackoverflow.com/questions/4130194/what-is-the-difference-between-task-and-thread 回答一: A tas ...

  2. Android中Calendar类的用法总结

    Calendar是Android开发中需要获取时间时必不可少的一个工具类,通过这个类可以获得的时间信息还是很丰富的,下面做一个总结,以后使用的时候就不用总是去翻书或者查资料了. 在获取时间之前要先获得 ...

  3. 【POJ 2230】 Watchcow

    [题目链接] http://poj.org/problem?id=2230 [算法] 欧拉回路 [代码] #include <algorithm> #include <bitset& ...

  4. B1786 [Ahoi2008]Pair 配对 逆序对+dp

    这个题有点意思,一开始没想到用dp,没啥思路,后来看题解才恍然大悟:k才1~100,直接枚举每个-1点的k取值进行dp就行了.先预处理出来sz[i][j]  i左边的比j大的数,lz[i][j]  i ...

  5. bzoj 1034 [ ZJOI 2008 ] 泡泡堂BNB —— 贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 一开始想了个很麻烦的贪心做法,对于每个 a[i],找第一个大于它的 b 匹配…… 然后 ...

  6. category中添加属性的简单方式

    一.概念扩充: 1.如我们所知,使用category是用来对现有类进行功能扩展,或者将类分成多模块的一种方式.由声明和实现两部分组成.可以单独写成Objiective-C File类型文件(包含.h和 ...

  7. Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts

    题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...

  8. Appium + python -always_allows弹窗

    from appium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.web ...

  9. var的变量提升的底层原理是什么?

    原理:JS引擎的工作方式是①先解析代码,获取所有被声明的变量:②然后在运行.也就是专业来说是分为预处理和执行两个阶段. 变量提升的定义:所有变量的声明语句都会被提升到代码头部,这就是变量提升. 例如: ...

  10. B - Cows and Poker Game

    Problem description There are n cows playing poker at a table. For the current betting phase, each p ...