如何简便改文件名

在出OI题的时候,有时候想要方便地把输入输出文件的文件名都改掉,类似于将a1.in,a2.in,...,a50.in都改成b1.in,b2.in,...,b50.in

我用gpt写了一个python代码

python 复制代码
import os

def rename_files(base_name, new_name, num_files):
    for i in range(1, num_files + 1):
        # 构建旧文件名和新文件名的完整路径
        old_in_file = f"{base_name}{i}.in"
        old_out_file = f"{base_name}{i}.out"
        new_in_file = f"{new_name}{i}.in"
        new_out_file = f"{new_name}{i}.out"

        # 检查并重命名 .in 文件
        if os.path.exists(old_in_file):
            os.rename(old_in_file, new_in_file)
            print(f"文件重命名成功!{old_in_file} => {new_in_file}")
        else:
            print(f"文件不存在:{old_in_file}")

        # 检查并重命名 .out 文件
        if os.path.exists(old_out_file):
            os.rename(old_out_file, new_out_file)
            print(f"文件重命名成功!{old_out_file} => {new_out_file}")
        else:
            print(f"文件不存在:{old_out_file}")

# 调用函数进行文件重命名,示例中假设 base_name 替换为 new_name,总共 num_files 个文件需要处理
base_name = "test"
new_name = "b"
num_files = 50  # 假设总共有20个文件需要处理
rename_files(base_name, new_name, num_files)

将这个代码放在要改名字的输入输出文件的同目录下,base_name写上原文件名,new_name写上新文件名,num_files写上文件个数

然后在文件所在文件夹进入终端,运行类似于

bash 复制代码
python ./changeFilename.py

就好了

这个需要你拥有python环境

相关推荐
何中应10 分钟前
Spring Boot整合Doris
java·数据库·spring boot
遨游DATA35 分钟前
Maven dependencyManagement 已声明却仍缺 Jar:如何验证最终运行包
java·spring boot·maven·故障排查·依赖管理
腻害兔39 分钟前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 认证与权限:RBAC + 数据权限,这套“门禁系统“到底怎么设计的?
java·vue.js·人工智能·产品经理·ai编程
Summer-Bright1 小时前
深度 | Agent 协议标准化:一场决定了 AI 经济底层规则的基础设施战争
java·数据库·人工智能·ai
我叫张小白。1 小时前
LangChain 结构化输出(Structured Output)技术文档
java·数据库·langchain
且随疾风前行.2 小时前
Android Binder 驱动 - 内核驱动层源码初探
android·网络·binder
猫猫不是喵喵.2 小时前
双亲委派机制与类加载过程
java·开发语言
大模型码小白2 小时前
向量化引擎与 AI 排障:当 SIMD 遇到异常检测,存储诊断的范式转移
java·大数据·数据库·人工智能·python
在水一缸2 小时前
当 AI 拥有了“核按钮”:深入解析 MCP 服务器与命令执行护栏
运维·服务器·人工智能·命令执行·智能体·ai安全·mcp
froyoisle2 小时前
阿里云免费 SSL 证书申请及配置
服务器·阿里云·ssl·网站