随笔 - 633,  文章 - 0,  评论 - 13,  阅读 - 48万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

代码:

复制代码
import os
import shutil

def copy_and_rename_files(src_directory, target_directory):
    # 确保目标目录存在
    if not os.path.exists(target_directory):
        os.makedirs(target_directory)
    
    # 遍历指定目录及其所有子目录
    for root, dirs, files in os.walk(src_directory):
        for file in files:
            if file.endswith('srt'):
                # 构建源文件的完整路径
                src_file_path = os.path.join(root, file)
                
                # 获取源文件所在的文件夹的相对路径
                relative_path = os.path.relpath(root, src_directory)
                
                # 构建目标文件夹的完整路径
                target_folder_path = os.path.join(target_directory, relative_path)
                
                # 如果目标文件夹不存在,则创建它
                if not os.path.exists(target_folder_path):
                    os.makedirs(target_folder_path)
                
                # 获取源文件所在的文件夹名
                folder_name = os.path.basename(root)
                
                # 构建新文件名,格式为:文件夹名_zhong_原文件名
                new_file_name = f"{folder_name}_zhong_{file}"
                
                # 构建目标文件的完整路径
                dest_file_path = os.path.join(target_folder_path, new_file_name)
                
                # 复制文件,并重命名
                shutil.copy(src_file_path, dest_file_path)
                print(f"Copied {src_file_path} to {dest_file_path}")

# 使用示例
# 将 "/path/to/source/directory" 替换为你的源目录路径
# 将 "/path/to/target/directory" 替换为你的目标目录路径
copy_and_rename_files("/path/to/source/directory", "/path/to/target/directory")
复制代码

 

同一个目录下:

复制代码
import os
import shutil

def copy_and_rename_files_in_same_dir(src_directory):
    # 遍历指定目录及其所有子目录
    for root, dirs, files in os.walk(src_directory):
        for file in files:
            if file.endswith('srt'):
                # 获取源文件所在的文件夹的名称
                folder_name = os.path.basename(root)
                
                # 构建新文件名,格式为:文件夹名_zhong_原文件名
                new_file_name = f"{folder_name}_zhong_{file}"
                
                # 构建源文件的完整路径
                src_file_path = os.path.join(root, file)
                
                # 构建目标文件的完整路径,确保它与源文件在同一目录下
                dest_file_path = os.path.join(root, new_file_name)
                
                # 复制文件,并重命名
                shutil.copy(src_file_path, dest_file_path)
                print(f"Copied {src_file_path} to {dest_file_path}")

# 使用示例
# 将 "/path/to/your/directory" 替换为你的源目录路径
copy_and_rename_files_in_same_dir("/path/to/your/directory")
复制代码

 

posted on   大话人生  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2020-05-01 Vue安装vuex
2020-05-01 Successfully created project 'vueSecond' on GitHub, but initial push failed: unable to access
2020-05-01 pycharm登录githu报443
点击右上角即可分享
微信分享提示