python 文件合并小工具

工作中,小文件太多,所以就写个python脚本专门用来做文件合并,默认是10个文件合并成一个文件,话不多少,看代码

#!/usr/bin/python

# -*- coding: utf-8 -*-

__copyright__ = 'Copyright (c) 2017, newzol.cn'

__author__ = 'new zol'

__version__ = '1.0.0'

import os

import fnmatch

import datetime

DEPTH_FILE='' #新文件生成目录

SOURCE_FILE_NAME_PAT='ACCUM*' #文件名匹配

filelist = [] #文件保存列表

if __name__ == '__main__':

print __copyright__

print __version__

print '开始处理目录:',os.getcwd()

now = datetime.datetime.now()

print("task start time:",now.strftime('%Y-%m-%d %H:%M:%S'))

try:

iTotalFile = 0

iCount=0

iNewfile=0

bFlag = False

global newfile

srcfile = os.getcwd()

filelist = os.listdir(srcfile)

for filename in filelist:

if not fnmatch.fnmatch(filename, 'ACCU*'):

continue

if os.path.isfile(os.path.join(srcfile,filename)):

iTotalFile+=1

if bFlag==False:

newfile = open(os.path.join(DEPTH_FILE,filename),'w')

bFlag = True

fp = open(os.path.join(srcfile,filename),'r')

for line in fp.readlines():

newfile.write(line.replace('\n',''))

newfile.write('\n')

iCount += 1

if iTotalFile % 10 == 0:

newfile.close()

iNewfile += 1

bFlag = False

if iTotalFile % 10 != 0:

newfile.close()

iNewfile += 1

print '处理文件:',iTotalFile

print '生成文件:',iNewfile

print '总话单数:',iCount

end = datetime.datetime.now()-now

print '任务总耗时:',end.seconds,' 秒'

except Exception as e:

print(e)

  • 发表于:
  • 原文链接:http://kuaibao.qq.com/s/20171215G0UNXH00?refer=cp_1026

扫码关注云+社区