python 批量替换文件名跟文件内容 - Go语言中文社区

python 批量替换文件名跟文件内容


下载地址


python gui模型批量替换文件明跟文件内容

1.导入需要的包


都pip 就行


    def TwoButton(self,event):
        path = self.names
        filelist=os.listdir(path)
        for files in filelist:#遍历所有文件
            Olddir=os.path.join(path,files);#原来的文件路径
            if os.path.isdir(Olddir):#如果是文件夹则跳过
                continue;
            filename=os.path.splitext(files)[0];#文件名
            filetype=os.path.splitext(files)[1];#文件扩展名
            file_data = ""
            txt = codecs.open(path+"\"+filename+filetype,'rb','gbk').readlines()  #打开文件,读入每一行
            file_data = ""
            with open(path+"\"+filename+filetype, "r",encoding="gbk") as f:  #打开文件
                for line in f:
                    da=self.biaotiText.GetValue() #获取到
                    da1=self.biaotiText1.GetValue()
                    biaoti2 =self.biaotiText2.GetValue()
                    print(biaoti2)
                    biaoti3 = self.biaotiText3.GetValue()
                    biaoti33 =self.biaotiText33.GetValue()
                    biaoti22 = self.biaotiText22.GetValue()
                    if(da!='标题1' and biaoti2=="标题2" and biaoti3=="标题3"):
                        line = line.replace(da,da1)
                    elif(da!='标题1' and biaoti2!="标题2" and biaoti3 =="标题3"):
                        line = line.replace(da,da1).replace(biaoti2,biaoti22)
                        print(line)
                    elif(da!='标题1' and biaoti2!="标题2" and biaoti3!="标题3"):
                        line = line.replace(da,da1).replace(biaoti2,biaoti22).replace(biaoti3,biaoti33)


                    file_data += line


            with open(path+"\"+filename+filetype,'w',encoding="gbk") as f:
                f.write(file_data)


            link=self.userText.GetValue() #这里是Unicode编码


            newpat =self.userText1.GetValue() #这里是Unicode编码
            news = re.sub(link,newpat,filename) #sub 是正则表达是里面的替换   把filename 里面的link 替换成newpat
            nn = news.replace("]","").replace("[","")
            Newdir=os.path.join(path,nn+filetype);#新的文件路径


            os.rename(Olddir,Newdir);#重命名

 
if __name__ == '__main__':  
    frame = wx.PySimpleApp()  
    app = DirDialog()  
    app.Show()  

    frame.MainLoop()  

代码下载 代码

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_33403388/article/details/79321563
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 20:43:18
  • 阅读 ( 900 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢