如何简便改文件名

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

相关推荐
weiwx831 分钟前
Nginx location 和 proxy_pass 配置详解
服务器·网络·nginx
毕设源码-朱学姐12 分钟前
【开题答辩全过程】以 基于Android的便民系统的设计与实现为例,包含答辩的问题和答案
android
鬼蛟22 分钟前
Spring————事务
android·java·spring
西门吹-禅1 小时前
【sap fiori cds up error】
java·服务器·sap cap cds
敲代码的嘎仔1 小时前
Java后端面试——SSM框架面试题
java·面试·职场和发展·mybatis·ssm·springboot·八股
大傻^2 小时前
Spring AI Alibaba RAG实战:基于向量存储的检索增强生成
java·人工智能·spring
大傻^2 小时前
Spring AI Alibaba 快速入门:基于通义千问的AI应用开发环境搭建
java·人工智能·后端·spring·springai·springaialibaba
伯恩bourne2 小时前
Google Guava:Java 核心工具库的卓越之选
java·开发语言·guava
qq_170264752 小时前
unity出安卓年龄分级的arr包问题
android·unity·游戏引擎
小王不爱笑1322 小时前
Spring 基础核心
java