如何简便改文件名

在出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环境

相关推荐
小王不爱笑13220 分钟前
LangChain4J 整合多 AI 模型核心实现步骤
java·人工智能·spring boot
西凉的悲伤21 分钟前
spring-boot-starter-validation使用注解进行参数校验
java·spring boot·参数校验·validation·注解校验参数
LucDelton34 分钟前
Java 读取无限量文件读取的思路
java·运维·网络
夹锌饼干43 分钟前
mysql死锁排查流程--(处理mysql阻塞问题)
java·mysql
不急不躁12343 分钟前
Android16 GTS GtsPermissionTestcases 测试,跳过权限检查
android
小信丶1 小时前
@EnableTransactionManagement注解介绍、应用场景和示例代码
java·spring boot·后端
To Be Clean Coder1 小时前
【Spring源码】createBean如何寻找构造器(四)——类型转换与匹配权重
java·后端·spring
Kaede61 小时前
提示dns服务器未响应,需要做哪些事?
运维·服务器
CRUD酱1 小时前
CentOS的yum仓库失效问题解决(换镜像源)
linux·运维·服务器·centos
-孤存-1 小时前
SpringBoot核心注解与配置详解
java·spring boot·后端