如何简便改文件名

在出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 小时前
Linux — 进程控制
android·linux·运维
budingxiaomoli10 小时前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫10 小时前
Springboot SSO系统设计文档
java·spring boot·后端
未若君雅裁10 小时前
MySQL高可用与扩展-主从复制读写分离分库分表
java·数据库·mysql
学习中.........11 小时前
从扰动函数的变化,感受红黑树带来的性能提升
java
计算机安禾11 小时前
【c++面向对象编程】第24篇:类型转换运算符:自定义隐式转换与explicit
java·c++·算法
我星期八休息11 小时前
Linux系统编程—基础IO
linux·运维·服务器·c语言·c++·人工智能·算法
Shingmc311 小时前
【Linux】数据链路层
linux·服务器·网络
weixin1997010801612 小时前
【保姆级教程】淘宝/天猫商品详情 API(item_get)接入指南:Python/Java/PHP 调用示例与 JSON 返回值解析
java·python·php
a7520662812 小时前
零基础实操:小龙虾 AI OpenClaw 接入 Kimi 详细步骤
运维·服务器