代码:

import os

def change_extension(directory, old_suffix, new_suffix):
    """
    遍历指定目录及其子目录,将所有后缀为 old_suffix 的文件修改为 new_suffix。
    """
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith(old_suffix):
                old_name = os.path.join(root, file)
                new_name = old_name[:-len(old_suffix)] + new_suffix
                os.rename(old_name, new_name)
                print(f"Renamed '{old_name}' to '{new_name}'")

# 使用示例
# 指定要搜索的目录
directory_to_search = "E:\edge下载"
# 要修改的旧后缀名
old_suffix = ".m4s"
# 要修改成的新后缀名
new_suffix = ".mp4"

# 执行函数
change_extension(directory_to_search, old_suffix, new_suffix)

 

posted on 2024-04-24 02:42  大话人生  阅读(9)  评论(0编辑  收藏  举报