如何简便改文件名

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

相关推荐
Awkwardx15 分钟前
Linux系统编程—线程同步与互斥
linux·服务器
熊小猿36 分钟前
在 Spring Boot 项目中使用分页插件的两种常见方式
java·spring boot·后端
paopaokaka_luck40 分钟前
基于SpringBoot+Vue的助农扶贫平台(AI问答、WebSocket实时聊天、快递物流API、协同过滤算法、Echarts图形化分析、分享链接到微博)
java·vue.js·spring boot·后端·websocket·spring
老华带你飞1 小时前
机器人信息|基于Springboot的机器人门户展示系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·机器人·论文·毕设·机器人门户展示系统
notion20251 小时前
Adobe Lightroom Classic下载与安装教程(附安装包) 2025最新版详细图文安装教程
java·数据库·其他·adobe
rengang662 小时前
351-Spring AI Alibaba Dashscope 多模型示例
java·人工智能·spring·多模态·spring ai·ai应用编程
小蒜学长2 小时前
springboot酒店客房管理系统设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
晨陌y2 小时前
深入剖析:仓颉语言的性能优化核心技术
android·性能优化·仓颉
lang201509282 小时前
Spring MVC配置全解析
java·spring·mvc
xhbh6662 小时前
【实战总结】MySQL日期加减大全:日期计算、边界处理与性能优化详解
android