利用Python批量对文件名中部分替换的代码

import os

def batch_rename(directory, old_substring, new_substring):
    for filename in os.listdir(directory):
        if old_substring in filename:
            new_filename = filename.replace(old_substring, new_substring)
            old_path = os.path.join(directory, filename)
            new_path = os.path.join(directory, new_filename)
            os.rename(old_path, new_path)

# 指定目录、旧的子字符串和新的子字符串
directory = '/path/to/directory'  # 修改为你要修改文件名的目录路径
old_substring = 'old'  # 修改为你要替换的旧子字符串
new_substring = 'new'  # 修改为你要替换的新子字符串

# 执行批量替换
batch_rename(directory, old_substring, new_substring)

一行代码修改文件后缀

ren *.TP7 *.txt
将代码粘贴到新建文本中,调整自己想要的格式后保存,随后将文本.txt的后缀修改文.bat,最后双击运行。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值