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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值