Python复制文件及子目录文件到指定目录

import os
import re
def readconfig():
    with open("./conf.txt", "r") as f:
        data = f.readlines()
    for item in data:
        if 'file' in item:
            file = re.search("file \= (.+)", item).group(1)
        elif 'src =' in item:
            src = re.search("src \= (.+)", item).group(1)
        elif 'dest =' in item:
            dest = re.search("dest \= (.+)", item).group(1)
        elif  'port1 =' in item:
            port1 = re.search("port1 \= (.+)", item).group(1)
        elif  'port2 =' in item:
            port2 = re.search("port2 \= (.+)", item).group(1)
        elif  'listen_port =' in item:
            listen_port= re.search("listen_port \= (.+)", item).group(1)
    return file,src,dest,port1,port2,listen_port
def copy_file(sourcePath,targetPath):
    if not os.path.exists(sourcePath):
        return
    if not os.path.exists(targetPath):
        os.makedirs(targetPath)
    for fileName in os.listdir(sourcePath):
        absourcePath = os.path.join(sourcePath, fileName)
        # 拼接目标文件或者文件加的绝对路径
        abstargetPath = os.path.join(targetPath, fileName)
        # 判断原文件的绝对路径是目录还是文件
        if os.path.isdir(absourcePath):
            # 是目录就创建相应的目标目录
            #os.makedirs(abstargetPath)
            # 递归调用getDirAndCopyFile()函数
            copy_file(absourcePath, abstargetPath)
        # 是文件就进行复制
        if os.path.isfile(absourcePath):
            rbf = open(absourcePath, "rb")
            wbf = open(abstargetPath, "wb")
            while True:
                content = rbf.readline(1024 * 1024)
                if len(content) == 0:
                    break
                wbf.write(content)
                wbf.flush()
            rbf.close()
            wbf.close()
if __name__ == '__main__':
    filename,src,dest,port1,port2,listen_port = readconfig()
    copy_file(src,dest)
    pathtemp1 = dest+'/conf/'+filename+'.cfg'
    pathtemp2 = os.path.join(dest,filename+'.py')
    for line in open(pathtemp2):
        if 'base.start_server' in item:
            temp1 = line.split(',')
            item  = temp1[0]+','+ port1+','+potr2+')'
    judge=0
    for line in open(pathtemp1):
        if '"name" : "./log/' in item:
            temp1 = line.split('log/')
            item = temp1[0]+'log/'+filename+'",'
        if '"listen_port" :' in item:
            temp1 = line.split(':')
            item  = temp1[0]+' :'+listen_port+','
        if '"listen_port" :{' in item:
            judge =1
        if judge == 1:
            temp1 = line.split(':')
            line = '                  '+port1+':1'
            judge=0

包含改文件替换文件的部分内容

你可能感兴趣的:(python)