Python : 批量替换代码文件内容,批量移动/覆盖文件

使用背景

批量代码文件内容修改,可用在生成代码工具上。
例如在设备端GUI页面显示上,很多是通过GUI上位机软件自动生成代码,然后把生成的代码copy到开发工程中编译使用。

但由于种种原因,存在生成的代码与实际工程有些不兼容的情况,则生成的代码不能直接copy使用。。

我之前遇到的是每次都需要改动一部分代码及文件名,才能放到工程正确编译,而GUI上位机是官方提供的无法修改。。。这就导致每次改UI 生成的所有代码就得做一次修改,手动做太麻烦,所以通过一个工具自动化实现上面步骤。

工具主要功能:检索并替换目录中所有的.c/.cpp, .h/.hpp文件及相关内容,之后自动copy并覆盖到指定目录。

实际应用中 比如中有个头文件 或变量名改动了:则通过工具一键替换,非常方便且高效,因为会检索目录中所有文件的全部内容

先上图:

在这里插入图片描述

代码解析 - getCppHppFileList

getCppHppFileList 是通过os 的api 把目录的所有文件名作为 list return,既获取目录中所有的 代码文件

def getCppHppFileList(code_path):
    _files = {}
    list = os.listdir(code_path) 
    for i in range(0, len(list)):
        path = os.path.join(code_path, list[i])
        if os.path.isfile(path):
            (filepath, tempfilename) = os.path.split(path)
            (filename, extension) = os.path.splitext(tempfilename)
            if extension == '.cpp' or extension == '.c' or extension == '.hpp' or extension == '.h':
                _files[list[i]] = tempfilename
    return _files

代码解析 - replace_StrInFile

replace_StrInFile 就是遍历文件内容,找到要替换的str 并且执行替换操作,使用了with 更加安全的操作文件。

def replace_StrInFile(file, Old, New):
    file_data =""
    isconfigLine = 0
   
    with open(file, "r") as f:
        for line in f:
            if Old in line:
                isconfigLine = 1
                line = "#include \""+New+"\"\n"
                print("replaced "+New+" in file: "+file)
            
            file_data += line

    with open(file, "w") as f:
        f.write(file_data)
        

这里只是替换了头文件名(因为我实际的代码是头文件名要改)

如果替换任意字符,使用下面方法,,而且上面方法是遍历改完后整个文件write,下面方法使用writelines效率会好一些:

def replace_StrinFile(FileName, OldStr, NewStr):
    file = open(FileName, 'r')
    alllines = file.readlines()
    file.close()
    file = open(FileName, 'w+')
    for eachline in alllines:
        a = re.sub(OldStr, NewStr, eachline)
        file.writelines(a)
    file.close()

代码解析 - reName_File

reName_File 直接使用shutil.copyfile(src, dst), 而移动并覆盖文件用的shutil.copy(src, dst)

def reName_File(srcFile, dstFile):
    try:
       shutil.copyfile(srcFile, dstFile)
    except IOError as e:
       print("Unable to backup file. %s" % e)
       exit(1)
    except:
       print("Unexpected error:", sys.exc_info())
       exit(1)
    else:
        print("reName file: "+dstFile+" success!\r")
        

代码解析 -Tkinter 图形界面

界面很简单,就是一个Text框和一个Button用来选择目标文件目录

这个UI代码结构也是我之前其他工具沿用的。。

def selectBtn():
     filename = tkinter.filedialog.askdirectory()
     if filename != '':
        intro.config(text =filename);
        moveCodeFilesToDstFolder(filename)
        btn.config(text = "替换完成, 自动关闭窗口")
        root.destroy()
     else:
        intro.config(text = "没有选择任何文件夹");

root = Tk()
root.geometry('600x300')
root.title('AutoReplace2.0')
root.config(bg='#f0ffff')
intro = Label(root, text='点击按钮选择要Copy覆盖的目录', \
              bg='#f0ffff', \
              fg='black', \
              font=('华文新魏', 10), \
              width=50, \
              height=5)

intro.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.3)

btn = Button(root, text="选择待替换目录,完成自动关闭窗口", command=selectBtn, \
             fg='red', \
             font=('华文新魏', 14), \
             width=20, \
             height=5)

btn.place(relx=0.1, rely=0.5, relwidth=0.8, relheight=0.2)

root.mainloop()

完整代码

#/usr/bin/python
#coding:utf-8

"""
To replace all include file name of embedded wizard generated code
"""
__author__ = 'HowardXue 2020.06.10'

import fileinput
import os
import time
import shutil
from tkinter import *
import tkinter.filedialog
#import re
#import io

core_str    = """Core.h"""
guiCore_str = """GUI_Core.h"""
res_str     = """Resources.h"""
guiRes_str  = """GUI_Resources.h"""


SrcfilePath  = "./GeneratedCode"

def getCppFileList(code_path):
    _files = {}
    list = os.listdir(code_path) 
    for i in range(0,len(list)):
           path = os.path.join(code_path,list[i])
           if os.path.isfile(path):
                (filepath,tempfilename) = os.path.split(path)
                (filename,extension) = os.path.splitext(tempfilename)
                if extension == '.c' or extension == '.cpp':
                    _files[list[i]] = tempfilename
    return _files


def getCppHppFileList(code_path):
    _files = {}
    list = os.listdir(code_path) 
    for i in range(0, len(list)):
        path = os.path.join(code_path, list[i])
        if os.path.isfile(path):
            (filepath, tempfilename) = os.path.split(path)
            (filename, extension) = os.path.splitext(tempfilename)
            if extension == '.cpp' or extension == '.c' or extension == '.hpp' or extension == '.h':
                _files[list[i]] = tempfilename
    return _files

def replace_StrInFile(file, Old, New):
    file_data =""
    isconfigLine = 0
   
    with open(file, "r") as f:
        for line in f:
            if Old in line:
                isconfigLine = 1
                line = "#include \""+New+"\"\n"
                print("replaced "+New+" in file: "+file)
            
            file_data += line

    with open(file, "w") as f:
        f.write(file_data)
        
def reName_File(srcFile, dstFile):
    try:
       shutil.copyfile(srcFile, dstFile)
    except IOError as e:
       print("Unable to backup file. %s" % e)
       exit(1)
    except:
       print("Unexpected error:", sys.exc_info())
       exit(1)
    else:
        print("reName file: "+dstFile+" success!\r")


def autoReplaceNameFile():
    os.chdir(SrcfilePath)
    cFilelist = getCppFileList(os.getcwd())
    for file in cFilelist.values():
        # print(file)
        replace_StrInFile(file, core_str, guiCore_str)
        replace_StrInFile(file, res_str, guiRes_str)

    reName_File("Core.c", "GUI_Core.c")
    reName_File("Core.h", "GUI_Core.h")
    reName_File("Resources.c", "GUI_Resources.c")
    reName_File("Resources.h", "GUI_Resources.h")
    os.chdir("../")
    print("\n AutoReplaceName Finished...")


def moveCodeFilesToDstFolder(DstfilePath):
    os.chdir(SrcfilePath)
    #print(DstfilePath)
    cFilelist = getCppHppFileList(os.getcwd())
    for file in cFilelist.values():
        #print(file)
        shutil.copy(file, DstfilePath)
    os.chdir("../")

    print("\nMove all .c/.h file \n from:" + os.getcwd() + "\n  to :" + DstfilePath  + " Success!")

def selectBtn():
     filename = tkinter.filedialog.askdirectory()
     if filename != '':
        intro.config(text =filename);
        moveCodeFilesToDstFolder(filename)
        btn.config(text = "替换完成, 自动关闭窗口")

        root.destroy()
     else:
        intro.config(text = "没有选择任何文件夹");



if __name__ == "__main__":
    root = Tk()
    root.geometry('600x300')
    root.title('AutoReplace2.0')
    root.config(bg='#f0ffff')

    autoReplaceNameFile()

    intro = Label(root, text='点击按钮选择要Copy覆盖的目录', \
                  bg='#f0ffff', \
                  fg='black', \
                  font=('华文新魏', 10), \
                  width=50, \
                  height=5)

    intro.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.3)

    btn = Button(root, text="选择待替换目录,完成自动关闭窗口", command=selectBtn, \
                 fg='red', \
                 font=('华文新魏', 14), \
                 width=20, \
                 height=5)

    btn.place(relx=0.1, rely=0.5, relwidth=0.8, relheight=0.2)

    root.mainloop()
    print("\nAll Finished, this window will auto close after 10s...")
    time.sleep(10)









博主热门文章推荐:

一篇读懂系列:

LoRa Mesh系列:

网络安全系列:

嵌入式开发系列:

AI / 机器学习系列:


在这里插入图片描述

其他好玩的python脚本

Python实现自动发送邮件 --自动抓取博客/网站中留言的邮箱并发送相应邮件
Python自动生成代码 - 通过tkinter图形化操作并生成代码框架
Python解析CSV数据 - 通过Pandas解析逻辑分析仪导出的CSV数据
Python通过Django搭建网站执行Lua脚本 (实现数据解析)

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HowieXue

求打赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值