python批量替换文件后缀名

 

仅供python环境使用,快捷方便直接一键式

 

import os

# 指定目录
directory = r'F:\backups\my\chajian'

# 旧后缀和新后缀
old_ext = '.txt'
new_ext = '.dat'

# 遍历指定目录
for filename in os.listdir(directory):
    # 检查文件是否以旧后缀结尾
    if filename.endswith(old_ext):
        # 构造旧文件的完整路径
        old_file = os.path.join(directory, filename)
        # 构造新文件名
        new_filename = filename[:-len(old_ext)] + new_ext
        # 构造新文件的完整路径
        new_file = os.path.join(directory, new_filename)
        # 重命名文件
        os.rename(old_file, new_file)
        print(f'Renamed "{old_file}" to "{new_file}"')

image

 

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容