python 合并文件夹里同类型文件

import os

# 批量文件目录
srcdir = "G:\\t2"
# 合成文件地址和名字
destpath = "G:\\t2.txt"
# 批量文件是否包含头文件
containTitle = True
filelist = os.listdir(srcdir)
destfile = open(destpath, 'w')
topfileidx = 0
for item in filelist:
    filepath = srcdir + '/' + item
    line = 0
    for txt in open(filepath, 'r'):
        if containTitle == False or (containTitle == True and (topfileidx == 0 and line == 0) or line == 1):
            destfile.write(txt)
        line += 1
    topfileidx += 1
destfile.close()

你可能感兴趣的:(python 合并文件夹里同类型文件)