利用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,最后双击运行。

你可能感兴趣的:(python,开发语言)