核心功能

Python实现内容替换的脚本

实现代码



import os


import re





'''


1、编写复制函数,实现复制文件夹


|--思路:获取目录下文件和目录信息,遍历并做复制操作


2、定义源目录路径(源文件夹包含文件和子目录)


3、定义目标路径


4、调用函数,传参源和目标目录


'''








def dir_full_copy(source, target):


    source = source


    target = target


    # 创建目标文件夹


    if not os.path.isdir(target):


        os.makedirs(target)


    # 获取目录信息,得到目录下的子目录名和文件名+后缀名


    dir_list = os.listdir(source)


    #print(dir_list)


    # 遍历目录下文件或文件夹


    for i in dir_list:


        # 拼接当前文件或子目录的完整路径


        i_path = os.path.join(source, i)


        print('i_path', i_path)


        # 判断是否目录


        # 如果是目录,则在目标目录新建目录, 并调用dir_full_copy进入源子目录复制里面的文件


        if os.path.isdir(i_path):


            # 拼接目标子目录完整路径


            target_child_dir = os.path.join(target_dir, i)


            print('target child dir', target_child_dir)


            # 在目标目录下新建相同的子目录


            #os.mkdir(target_child_dir)


            if not os.path.isdir(target_child_dir):


                os.makedirs(target_child_dir)            


            # 递归调用,子目录为传参


            dir_full_copy(i_path, target_child_dir)


        # 如果不是目录,则复制到目标文件夹下


        else:


            #print('else is file', i)


            # 目标文件


            target_file = os.path.join(target, i)


            print('target file', target_file)


            # 二进制&读模式打开源文件


            # 因为源目录下不一定只有txt文本,如果是图片之类的,只能通过二进制来读取


            with open(i_path, 'rb') as str:


                source_data = str.read()


                # 二进制&写模式打开目标文件,并写入数据


                with open(target_file, 'wb') as str2:


                    str2.write(source_data)








'''


1、编写复制函数,实现复制文件夹


|--思路:获取目录下文件和目录信息,遍历并做复制操作


2、定义源目录路径(源文件夹包含文件和子目录)


3、定义目标路径


4、调用函数,传参源和目标目录


'''








def java_convert_golang(source, target):


    source = source


    target = target


    # 创建目标文件夹


    if not os.path.isdir(target):


        os.makedirs(target)


    # 获取目录信息,得到目录下的子目录名和文件名+后缀名


    dir_list = os.listdir(source)


    #print(dir_list)


    # 遍历目录下文件或文件夹


    for i in dir_list:


        # 拼接当前文件或子目录的完整路径


        i_path = os.path.join(source, i)


        print('i_path', i_path)


        # 判断是否目录


        # 如果是目录,则在目标目录新建目录, 并调用dir_full_copy进入源子目录复制里面的文件


        if os.path.isdir(i_path):


            # 拼接目标子目录完整路径


            target_child_dir = os.path.join(target_dir, i)


            print('target child dir', target_child_dir)


            # 在目标目录下新建相同的子目录


            #os.mkdir(target_child_dir)


            if not os.path.isdir(target_child_dir):


                os.makedirs(target_child_dir)            


            # 递归调用,子目录为传参


            java_convert_golang(i_path, target_child_dir)


        # 如果不是目录,则复制到目标文件夹下


        else:


            #print('else is file', i)


            # 目标文件


            target_file = os.path.join(target, i)


            print('target file', target_file)


            # 二进制&读模式打开源文件


            # 因为源目录下不一定只有txt文本,如果是图片之类的,只能通过二进制来读取


            with open(i_path, 'rb') as str:


                source_data = str.read()


                # 二进制&写模式打开目标文件,并写入数据


                with open(target_file, 'wb') as str2:


                    str2.write(source_data)





def java_token_replace(source,target):


    


    


    s = "正则表达式"


    p = re.compile(s)


    


    # 查找


    mf1 = p.search(source)


    mf2 = p.findall(source)


    mf3 = p.finditer(source)


    


    # 替换


    ms = p.sub(source)


    ms2 = p.subn(source)


    


    # 分割


    mp = p.split("检测内容")


    





# 定义源目录和目标


# (注意,如果是Windows目录,需要加在路径分隔符\前加转义字符或者在最前面引号前加r)


source_dir = 'D:/source/shop-saas3/back-end-code/fz-platform'


target_dir = 'D:/codebase/python/target'


# 调用复制函数,传参源目录及目标目录


java_convert_golang(source_dir, target_dir)