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
仅此记录我逝去的一天!
相关推荐
哈嘿Blog 2020-10-26
明月清风精进不止 2020-07-05
PythonMaker 2020-07-05
xirongxudlut 2020-06-28
kkpiece 2020-06-16
qscool 2020-06-12
CloudXli 2020-06-11
vs00ASPNET 2020-06-09
Dimples 2020-06-08
kuoying 2020-06-07
JJandYY 2020-05-31
Wyt00 2020-05-30
liuyh 2020-04-03
CloudXli 2020-05-11
世樹 2020-05-11
bizercsdn 2020-05-10
joyjoy0 2020-05-09