针对配置文件进行切分,重组,每隔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
以上就是本文的全部内容,希望对大家的学习有所帮助
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!