“人生的游戏不在于拿了一副好牌,而在于怎样去打好坏牌”
——【美】赖斯利
python教学第六期
大家还记得上期的项目吗 点我复习一下
这次我优化了一下代码,同时修复了部分bug加入了新的功能
下面对比一下大概的数据
v0.0.0(脚本) | v0.1.1(脚本) | v0.0.0(打包程序) | v0.1.1(打包程序) | |
大小 | 432.7KB | 433.3KB | 432.7KB | 9.3MB |
功能 | 普匹+不匹+正则匹配 获取文件夹、文件名 | 普匹+不匹 +正则匹配 获取文件夹、文件名、文件路径、一键修改后缀 | 普匹+不匹 获取文件夹、文件名 | 普匹+不匹 获取文件夹、文件名、文件路径、一键修改后缀 |
当然大小并不是评价一个程序的标准,功能才是
这次更新大概做了以下几点(可在脚本md文件中查看修改记录)
在开始分析脚本前,我先进行以下普及
后缀,格式为 【文件名】.【后缀】,例如 Test.txt中,Text为文件名,txt为后缀
后缀会决定一个文件的打开方式,即决定了文件的类型
比如mp4是视频文件
mp3、flac都是音乐文件
这个是跟随我们大脑的想法来改变的吗?
并不是——
实际上,有时候我们会得到一些后缀与我没想法不同的文件
比如我们想要mp3文件,结果却给了我们一堆flac文件
这个时候最常用的办法是把.flac为后缀的文件全部改成.mp3
但是这个方法改三四个还好
如果这样呢
一个一个改恐怕不现实吧
这个时候,程序的作用就来了
这个脚本可以一键为指定路径下特定后缀文件修改后缀
并且文件名都将保留,而除了指定后缀结尾的文件,不会对任何文件进行更改
这样要改文件后缀就方便了不少
下面是脚本
main.py文件
#!/usr/bin/python# -*- coding:utf-8 -*-import tkinter as tkimport toolfrom sys import executable,argvfrom os import execlfrom tkinter import messageboxmod = "Mismatch"window = tk.Tk()entry_Matching_formula = tk.Entry(window,width=50)label_Matching_formula = tk.Label(window,text="输入关键字")entry_Matching_formula.insert(0,"findall(keyword,text)")list_Matching_formula = [label_Matching_formula,entry_Matching_formula ]entry_match = tk.Entry(window,width=50)label_match = tk.Label(window,text="输入匹配式(语言python)")list_match = [entry_match,label_match]def get_entry(): return entry_root.get()def paint_list(data): for d in data: print(d)def General_matching_method(): global mod mod = "General_matching_method" for m,f in list_match,list_Matching_formula: f.pack_forget() m.pack()def Mismatch(): global mod mod = "Mismatch" for m,f in list_match,list_Matching_formula: m.pack_forget() f.pack_forget()def Advanced_matching(): global mod mod ="Advanced matching" for m,f in list_match,list_Matching_formula: m.pack_forget() f.pack()def match(data): if mod == "Mismatch": pass elif mod == "General_matching_method": text = entry_match.get() data = [d for d in data if text in d] else: text = entry_Matching_formula.get() tool.advanced_matching(str(text)) from matching import matching data =[matching(d) for d in data] while [] in data: data.remove([]) paint_list(data)class get(): def __init__(self): pass def get_root(self): self.top = get_entry() data = tool.file_name(self.top)["root"] print(data) def get_dirs(self): self.top = get_entry() data = tool.file_name(self.top)["dirs"] match(data) def get_files(self): self.top = get_entry() data = tool.file_name(self.top)["files"] match(data)def rename_(): # 此函数为2020年8月9日更新内容 global b_entry_rename, entry_rename def rename(): global b_entry_rename, entry_rename b_name = b_entry_rename.get() a_name = entry_rename.get() name_list = tool.file_name(get_entry())["files"] name_list = [tool.file_name(get_entry())["root"][-1] + "\\" + n for n in name_list] for root in name_list: tool.name(root,root.replace(b_name,a_name)) messagebox.showwarning("注意","已完成指定操作") restart_program() mod = "General_matching_method" tk.Label(window,text="将后缀为").pack() b_entry_rename = tk.Entry(window,width=20) b_entry_rename.insert(0,".") b_entry_rename.pack() tk.Label(window,text="替换为").pack() entry_rename = tk.Entry(window,width=20) entry_rename.insert(0,".") entry_rename.pack() tk.Button(window,text="更换",command=rename).pack()def restart_program(): # 此函数为2020年8月9日更新 python = executable execl(python, python, * argv)window.geometry("500x600")window.title("文件汇总工具")tk.Label(window,text="输入路径").pack()entry_root = tk.Entry(window,width=50)entry_root.pack()tk.Button(window,width=10,text="root",command=get().get_root).pack()tk.Button(window,width=10,text="dirs",command=get().get_dirs).pack()tk.Button(window,width=10,text="files",command=get().get_files).pack()radiobutton1 = tk.Radiobutton(window,text="高级匹配(正则匹配式)",value="Advanced matching",command=Advanced_matching)radiobutton2 = tk.Radiobutton(window,text="一般匹配法(关键字)",value="General matching method",command=General_matching_method)radiobutton3 = tk.Radiobutton(window,text="不匹配",value="Mismatch",command=Mismatch)radiobutton = [radiobutton1,radiobutton2,radiobutton3]rename_()for r in radiobutton: r.pack()window.mainloop()
tool.py文件
#!/usr/bin/python# -*- coding:utf-8 -*-from os import walk,renamefrom re import findalldef file_name(top): # 此函数为2020年8月9日改动 root,dirs,files = [],[],[] for r,d,f in walk(top=top,topdown=False): root.append(r) dirs.append(d) files.append(f) return {"root":root,"dirs":dirs[-1],"files":files[-1]}def newly_build(text): open("matching.py","w+").write("") with open("matching.py","a+") as w: data = text.split("\n") w.write('''from re import findalldef matching(text):''') for d in data: w.write(" data = ") w.write(d + "\n") w.write(" return data")def general_matching_method(text,keyword): data = findall(keyword,text) return datadef advanced_matching(matching_formula): newly_build(str(matching_formula))def name(src,dst): # 此函数为2020年8月9日更新 rename(src,dst)
然后详细讲一下修改过的部分、更新的部分
1.
def rename_(): # 此函数为2020年8月9日更新内容 global b_entry_rename, entry_rename def rename(): global b_entry_rename, entry_rename b_name = b_entry_rename.get() a_name = entry_rename.get() name_list = tool.file_name(get_entry())["files"] name_list = [tool.file_name(get_entry())["root"][-1] + "\\" + n for n in name_list] for root in name_list: tool.name(root,root.replace(b_name,a_name)) messagebox.showwarning("注意","已完成指定操作") restart_program() mod = "General_matching_method" tk.Label(window,text="将后缀为").pack() b_entry_rename = tk.Entry(window,width=20) b_entry_rename.insert(0,".") b_entry_rename.pack() tk.Label(window,text="替换为").pack() entry_rename = tk.Entry(window,width=20) entry_rename.insert(0,".") entry_rename.pack() tk.Button(window,text="更换",command=rename).pack()
这一段便是实现新功能的代码,通过tool模块来实现获取文件名,再合并出路径,最后匹配指定后缀的文件并且利用tool中更新的函数来实现更改后缀
2.
def restart_program(): # 此函数为2020年8月9日更新 python = executable execl(python, python, * argv)
这个实际上是一个常用的终止脚本的函数,因为还未讲过sys库和os库,这部分先不详细解释
3.
def file_name(top): # 此函数为2020年8月9日改动 root,dirs,files = [],[],[] for r,d,f in walk(top=top,topdown=False): root.append(r) dirs.append(d) files.append(f) return {"root":root,"dirs":dirs[-1],"files":files[-1]}
这一段有一些小修改,解决了路径错乱、找不到路径的问题
4.
def name(src,dst): # 此函数为2020年8月9日更新 rename(src,dst)
这一段不要看它短,却是新功能的核心之一
这个是os库下的rename方法,参数有两个,src与dst
src为修改前路径,dst为修改后路径
如果缺了这个函数新功能就一点用也没有了
然后为大家提供下载方式
同样统一解压码为wzdhwn
exe打包文件:https://wws.lanzous.com/iIjfBfgckwf
脚本:https://wws.lanzous.com/iyixQfgd5pe
详细的功能和大小见文章开头
如果没有安装python可以点击此处下载
正文到此结束,长按图片可以关注公众号