配置需要合并的文件夹或者文件,文件夹会合并其中的所有文件
配置需要合并的文件类型
配置写入文件的绝对路径
import os
from pathlib import Path
# 需要要写入的文件
modolList = [
# r"D:\my_data\03毕设\provider\activiti_deploy",
# r"D:\my_data\03毕设\provider\demo2",
# r"D:\my_data\03毕设\activiti-consumer\activiti",
# r"D:\my_data\03毕设\activiti-consumer\api"
r"D:\my_data\03毕设\provider",
# r"D:\my_data\03毕设\provider\demo2",
r"D:\my_data\03毕设\activiti-consumer",
r"D:\my_data\03毕设\ATC-activiti\src"
# r"D:\OneDrive - xdd307\毕业设计\gym\gym-management-system\src",
# r"D:\OneDrive - xdd307\毕业设计\gym\gym-management-system\pom.xml",
# r"D:\OneDrive - xdd307\毕业设计\gym\gym_management_system.sql"
]
# 写入的文件类型
checkFile = [
"java",
"xml",
"yml",
"html",
"sql",
"vue",
"js"
]
# 写入的文件
writeFileNam = r"D:\OneDrive - xdd307\userxdd.docx"
pre = '\\'
# 写入文件
def write(path, filename):
path = path + pre + filename;
# open打开文件
fb = open(
file=path,
mode="r", encoding="utf-8")
# 读取文件
ct = fb.read()
fb = open(file=writeFileNam, mode="a", encoding="utf-8")
fb.write("文件名称:" + filename + "\n")
fb.write(ct)
fb.close()
# 手动关闭文件
fb.close()
print("文件:" + path)
# 检查文件类型
def checkFileType(path, file_name):
# safux = file_name[file_name.index('.') + 1:]
# 获取文件后缀
safux = os.path.splitext(file_name)[1]
if safux != '':
safux = safux[1:]
else:
return
# 检查是不是需要添加的文件
for checkFileType in checkFile:
if safux == checkFileType:
write(path, file_name)
# 遍历文件夹
def getfiles(path):
for file_name in os.listdir(path):
if os.path.isdir(path + pre + file_name):
getfiles(path + pre + file_name)
else:
checkFileType(path, file_name)
# 开始检查支持文件
def modoulView(basePath):
# 检查是不是文件
if os.path.isdir(basePath):
# 是文件遍历文件
getfiles(basePath)
else:
# 不是文件,检查文件类型
checkFileType(os.path.split(basePath)[0], os.path.basename(basePath))
# 遍历需要查找的文件
for modouName in modolList:
print(modouName)
modoulView(modouName)