Python脚本实现代码复制、文件名、关键字自动替换

Python脚本实现代码复制、文件名、关键字自动替换

场景

今天遇到各个地市要统一某系统接口,由于各文件的命名为 XXXX_地市名,schema mapping等文件又放各自在同个文件夹里,要新做一套接口要一个个拎出来复制修改。由于本人比较懒,正好电脑上又有Python 3.6,听说python写脚本也不错,就试了一下

思路

由于大部分代码都是相同的,只需要修改某些关键字,那我需要实现的主要功能就是复制和修改文件

复制

新建一个中间文件夹作为容器用来放置复制出来的代码文件 通过 shutil.copyfile() 实现,再在判断里过滤下,过滤掉文件夹和不带关键字的文件

替换

分为文件名替换和文件内容关键字替换,os.repalce() 没什么好说的,关键在于调用.open()方法时注意先读再写 ,同时声明一下字符编码集

为什么要放到中间文件夹?

考虑到复制的目标文件夹文件较多,想看效果比较麻烦,还有修改文件名和内容的时候总不能把源文件都改了吧...

代码

话不多说上代码...

#包内文件名和内容替换
#这么多print只是为了在控制台装装逼,可删
#author fengld 2017.12.19 同日发布于开源中国,未经允许请勿转载
#e-mail: dasvenxx@gmail.com
#by python
import os , codecs , shutil
Path = r'E:\workspace\XXXX'#此处为工作空间中要批量复制和修改的位置
toPath = r'C:\Users\usr\Desktop\schemas_Temp'#中间文件夹
all_file_list = os.listdir(Path)
#文件名修改
Oldpart = "QYYB"#要替换的内容
Newpart = "NZJYB"#替换后的内容
#文件后缀修改 要用再修改
Oldposfix = r".*"
Newposfix = r".**"

#创建中间文件夹
def Createdir(src):
    os.mkdir(src)
    print("创建文件夹: "+src+" ...")

#删除中间文件夹
def Removedirandfile(src):
    shutil.rmtree(src)
    print("删除文件夹: "+src+" ...")

#批量修改文件名
def Modifyprefix(oldcontent,newcontent):
    for file_name in my_file_list:
        currentdir = os.path.join(toPath, file_name)
        if os.path.isdir(currentdir):#跳过文件夹
            continue
        fname = os.path.splitext(file_name)[0]
        ftype = os.path.splitext(file_name)[1]
        replname = fname.replace(oldcontent,newcontent)
        newname = os.path.join(toPath,replname+ftype)
        os.rename(currentdir,newname)
        print(" 重命名文件名..."+"将 "+fname+" 替换为 "+replname)
    print("重命名文件...finish")

#批量修改文件后缀,未启动
def Modifypostfix(oldftype,newftype,src,list):
    for file_name in list:
        currentdir = os.path.join(src, file_name)
        if os.path.isdir(currentdir):
            continue
        fname = os.path.splitext(file_name)[0]
        ftype = os.path.splitext(file_name)[1]
        if ftype == oldftype:
            newname = os.path.join(src,fname+newftype)
        os.rename(currentdir,newname)
        print("重命名:"+file_name+" ...")
    print("重命名文件后缀...finish")

#内容替换
def Modifycontent(oldcontent,newcontent):
    for file_name in my_file_list:
        currentdir = os.path.join(toPath, file_name)
        if os.path.isdir(currentdir):#跳过文件夹
            continue
        f = open(os.path.join(toPath,file_name),'r',encoding="utf-8")#读,注意编码集设置
        filecontent=""
        line = f.readline()
        while line:
            l=line.replace(oldcontent,newcontent)
            filecontent = filecontent+l
            line = f.readline()
        f.close()
        f = open(os.path.join(toPath,file_name),'w',encoding="utf-8")#写,注意编码集设置
        f.writelines(filecontent)
        f.close()
        print("file: "+file_name+" 替换内容...finish")
    print("内容替换...finish")

#移动文件
def Movetopath(src1 , src2 ,content,list):
    for file_name in list:
        currentdir = os.path.join(src1, file_name)
        if os.path.isdir(currentdir) or file_name.find(content) == -1:#跳过文件夹和其他文件
            continue
        shutil.copyfile(os.path.join(src1,file_name),os.path.join(src2,file_name) )
        print("file: " + file_name + " 拷贝...finish")
    print("拷贝文件...finish")

Createdir(toPath)
Movetopath(Path,toPath,Oldpart,all_file_list)
my_file_list = os.listdir(toPath)#复制后获取缓存文件夹中的所有文件名称
Modifyprefix(Oldpart,Newpart)
#my_file_list = os.listdir(toPath)
#Modifypostfix(Oldposfix,Newposfix,toPath,my_file_list)
my_file_list = os.listdir(toPath)#改名后获取缓存文件夹中的所有文件名称
Modifycontent(Oldpart,Newpart)
Movetopath(toPath,Path,Newpart,my_file_list)
Removedirandfile(toPath)
print("all finished ...")

如果要从指定文件夹复制到另一新文件夹,修改toPath路径,并关闭删除文件夹方法就可以了 如果还要改后缀,开启.Modifypostfix()方法
文件为Demo,在本地执行通过,异常处理没做优化

可能遇到的问题

  • 文件夹已存在引发创建失败无法继续
  • 文件已存在引发无法继续
  • 替换时没有替换不同大小写拼写的关键字
  • 删除文件或文件夹时由于文件正在被访问无法删除
  • ...

转载于:https://my.oschina.net/dasven/blog/1592247

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值