代码改变世界

txt文件分解为固定条数的文件

2016-05-03 11:18  LI桥IL  阅读(212)  评论(0编辑  收藏  举报
#-*- coding: UTF-8 -*-
# import linecache
# count = linecache.getline(filename,linenum)
# 读取文件某一行的内容
# str = linecache.getlines(filename)
# str为列表形式,每一行为列表中的一个元素
import sys
import linecache

reload(sys)
sys.setdefaultencoding( "utf-8" )

# 分解一个txt文件为固定条数的文件
def recount_txt_number(txtpath,file_output_name,number):
    f=open(txtpath,"r")
    filename=file_output_name+".txt"
    for i in range(1,len(f.readlines())+1):
        count= linecache.getline(txtpath,i)
        with open(filename,"a") as output:
            output.write(count)
        if i%number==0:
            filename=file_output_name+str(i)+".txt"
            print filename