编程语言
首页 > 编程语言> > python 对文件进行切分行

python 对文件进行切分行

作者:互联网

针对配置文件进行切分,重组,每隔30行为一段,进行重新生成功能。

#!/usr/local/python/bin/python
# coding=utf-8
import sys
import re
import os
f = open('config.conf','r')
#判断文件条数
def file_num(filename):
    num_col = 0
    with open(filename,'rb') as Fnum:
       while(Fnum.readline() !=''):
          num_col = num_col + 1
    return num_col
   

file_num = file_num('config.conf')
print '文件总条数:' + str(file_num)

i = 0 #设置计数器
while i < file_num :              #表示文件行数
    with open('/tmp/newfile/newfile'+str(i),'w') as f1:
        for j in range(0,30) : #这里设置每个子文件的大小
            if i <= file_num : #这里判断是否已结束,否则最后可能报错
                f1.writelines(f.readline())
                i = i+1
            else:
                break

filepath1='/tmp/newfile/'
def eachFile(filepath):
   num = 1
   pathDir = sorted(os.listdir(filepath))
   for allDir in pathDir:
        child = os.path.join('%s%s' % (filepath, allDir))
        file_name = child.decode('gbk')
        print file_name
        with open('/tmp/check.py','a+') as f2:
          fopen = open(file_name, 'r+')
          f2.writelines("# config.py sql" + str(num)  + " begin\n")
          count=0
          for line in fopen.readlines(): 
              if count == 0:
                f2.writelines(line)
                count= count+1
              else:
                f2.writelines("UNION ALL " + line)
          f2.writelines("# config.py sql" + str(num)  + " end\n")
          f2.writelines("\n")
          num = num + 1

eachFile(filepath1)

脚本说明:

目录路径:/tmp

配置文件:config.conf

准备目录:/tmp/newfile/

最终新的配置文件为:check.py

仅此记录我逝去的一天!

标签:文件,conf,配置文件,python,open,num,file,分行,col
来源: https://www.cnblogs.com/hello-wei/p/12754898.html