下载地址
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()
代码下载 代码