python合并多个文本文件

由于工作需要合并多个sql文件提供给客户作为初始化脚本,于是写了几行python代码代替手动处理

import os

path = "D:\\testfile\\3\\migration\\"
file_list = os.listdir(path)
lines = []
for filename in file_list:
    filepath = path + '/'
    filepath = filepath + filename
    file_handle = open(filepath, mode="r", encoding="utf-8")
    lines.extend(file_handle.readlines())
    file_handle.close()


write_handle = open("D:\\testfile\\3\\migration\\init.sql", mode="w", encoding="utf-8")
write_handle.writelines(lines)
write_handle.close()

你可能感兴趣的:(工作工具,python)