最近下载了一些动漫,每个文件名都带着广告,强迫症的我怎么能忍,几百个文件要改到啥时间,必须造它。
1、首先写代码
创建文件rename.py
import os
import sys
import typer
main = typer.Typer()
CUR_PATH = os.getcwd()
@main.command()
def replace_name(src: str = typer.Argument(..., help="替换源内容"),
dst: str = typer.Argument("", help="替换目标内容"),
count: int = typer.Option(-1, "--count", "-c", help="替换次数"),
depth: int = typer.Option(0, "--depth", "-d", help="遍历目录深度[0当前目录 1当前目录及子目录 ...]")
):
# 遍历当前目录下的所有文件
for root, dirs, files in os.walk(CUR_PATH, topdown=True):
if root.count(os.sep) - CUR_PATH.count(os.sep) > depth:
continue
for name in files:
if src and src in name:
src_name = os.path.join(root, name)
dst_name = os.path.join(root, name.replace(src, dst, count))
os.rename(src_name, dst_name)
print(f"==={src_name}\n==>{dst_name}\n")
if __name__ == "__main__":
main()
2、打包成exe程序
pyinstaller -F rename.py
3、配置环境变量
注意:打包程序的时候注意命名,不要跟系统或其它的名称重名,可以加个自己的前缀。
把打包好的程序,放在我指定的工具目录E:/common,然后配置系统环境变量
4、测试
在需要重命名文件夹中,按住shift+鼠标右键 在此处打开命令行窗口
rename "[人人影视字幕组]"
完美