python根据后缀筛选文件并读取内容
#!/usr/bin/python# -*- coding: utf-8 -*-# --------------------------------------------------# @Time : 2020/12/20 18:40# @Author : skywin886# @File : ls_and_cat.py# @Version : 1.0# --------------------
·
#!/usr/bin/python
# -*- coding: utf-8 -*-
# --------------------------------------------------
# @Time : 2020/12/20 18:40
# @Author : skywin886
# @File : ls_and_cat.py
# @Version : 1.0
# --------------------------------------------------
# --------------------------------------------------
import os
import subprocess
dir = '/etc/nginx/conf.d/'
list = os.listdir(dir) #列出文件夹下所有的目录与文件
file = open('./allConfd.conf','a') #结果增量写入文件
rule=".conf" #设置过滤后的文件类型,可以设置多个类型,为空则不过滤
#判断是否以xxx结尾,结果存入列表
ruleAll = []
for i in range(0,len(list)):
path = os.path.join(dir,list[i])
if path.endswith(rule):
ruleAll.append(path)
print(ruleAll)
#将列表中所有文件内容合并至一个文件
for x in ruleAll:
#换行分隔符
file.write("####################""\n")
#文件名写入文件
file.write("#"+ os.path.basename(x))
#文件内容写入文件
osFile = subprocess.check_output('cat %s%s' % (dir,os.path.basename(x)), shell=True, cwd="./")
file.write(osFile)
#文件写入关闭
file.close()
上图脚本为 将所有nginx配置合并为一个文件
更多推荐



所有评论(0)