您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用Python實現大文件分割和命名腳本的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
日志文件分割、命名
工作中經常會收到測試同學、客戶同學提供的日志文件,其中不乏幾百M一G的也都有,畢竟壓測一晚上產生的日志量還是很可觀的,xDxD,因此不可避免的需要對日志進行分割,通常定位問題需要針對時間點,因此最好對分割后的日志文件使用文件中日志的開始、結束時間點來命名,這樣使用起來最為直觀。
大文件分割
用法:
python split_big_file.py
輸入文件全路徑名
輸入期望的分割后每個小文件的行數
Just wait.
# -*- coding:utf-8 -*- import os,re,shutil import platform sys_name = platform.system().lower() SPLIT_CHAR = '\\' if sys_name.find('windows') != -1 else '/' print('input big files`s path:') _path = raw_input() names = [] pathes = [] if os.path.isfile(_path): print('is file') names.append(_path) else: print('is nothing') ''' elif os.path.isdir(_path): print('This is dir') pathes = os.listdir(_path) print('pathes='+str(pathes)) for i in range(len(pathes)): fullpath = _path+SPLIT_CHAR+pathes[i] print('fullpath='+fullpath) if os.path.isfile(fullpath): names.append(fullpath) files.append(open(fullpath).read().split('\n')) ''' print(len(names)) line_num = int(raw_input('every file`line num = ')) print('line number='+str(line_num)) for i in range(len(names)): _name = names[i] ori_name = _name.split(SPLIT_CHAR)[len(_name.split(SPLIT_CHAR))-1] dir_name = _name.replace(ori_name,'DIR_'+ori_name) dir_name = dir_name.replace('.','_') print ori_name print dir_name os.system('mkdir '+dir_name) count = 1 print '已處理:'+str(count)+'行' part_file = open(dir_name+SPLIT_CHAR+str(0)+'.part.txt','w') with open(_name, 'rb') as f: for line in f: if count%line_num == 0: part_file.close() part_file = open(dir_name+SPLIT_CHAR+str(int(count/line_num))+'.part.txt','w') part_file.write(line+'\n') count+=1 if count%100000 == 0: print '已處理:'+str(count)+'行' print '已處理:'+str(count)+'行' os.system('python ./get_name_logfile.py '+dir_name)
文件按照開始、結束行時間戳重命名
用法:
python get_name_logfile.py log.txt
python get_name_logfile.py logs
參數選擇文件或者文件夾均可,如果是文件夾,則會針對文件夾中的每個文件做處理(不會遞歸到文件夾下文件夾中的文件哦);
# -*- coding:utf-8 -*- import os,re,shutil import sys import platform sys_name = platform.system().lower() SPLIT_CHAR = '\\' if sys_name.find('windows') != -1 else '/' _path = sys.argv[1] names = [] files = [] pathes = [] if os.path.isfile(_path): print('is file') names[0] = _path elif os.path.isdir(_path): print('This is dir') pathes = os.listdir(_path) print('pathes='+str(pathes)) for i in range(len(pathes)): fullpath = _path+SPLIT_CHAR+pathes[i] print('fullpath='+fullpath) if os.path.isfile(fullpath): names.append(fullpath) else: print('is nothing') print(len(names)) # 日期格式 : 05-26 18:20:42.093 r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}' # # 05-26 18:20:43.093:r'\d{2}-\d{2} {1,}\d{2}:\d{2}:\d{2}.\d{1,10}' date_reg = r'\d{2}-\d{2} {1,}\d{2}:\d{2}:\d{2}.\d{1,10}' time_reg = r'\d{2}:\d{2}:\d{2}.\d{1,10}' for i in range(len(names)): _name = names[i] print('name='+_name) # head 嘗試在10行內查找日期 head_len = 10 start_time = '(start_time-' _file_ = open(_name, 'rb') reads = _file_.read() _file = reads.split('\n') if len(_file)/2 < 10: head_len = len(_file)/2 for j in range(head_len): res = re.search(date_reg, _file[j]) if res!=None and res.group(0)!=None: start_time = res.group(0) print('start_time='+start_time) break # tail tail_len = len(_file)-head_len end_time = '-end_time)' for j in range(len(_file)-1,tail_len-1,-1): res = re.search(time_reg, _file[j]) if res!=None and res.group(0)!=None: end_time = res.group(0) print('end_time='+end_time) break _file_.close() ori_name = _name.split(SPLIT_CHAR)[len(_name.split(SPLIT_CHAR))-1] print('ori_name='+ori_name) new_name = start_time.replace(':','-')+'__'+end_time.replace(':','-')+os.path.splitext(ori_name)[1] print('new_name='+new_name) print("copy %s %s" % (_name, _name.replace(ori_name,new_name))) #os.system ("copy %s %s" % (_name, _name.replace(ori_name,new_name))) shutil.copy(_name,_name.replace(ori_name,new_name)) os.system ("rm -rf "+_name)
關于“使用Python實現大文件分割和命名腳本的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。