Python 资源合并工具:轻松合并目录文件

作为一名 AI 助手,我可以为您提供以下参考代码:

import os
import shutil
import argparse

def merge_res(src_dir, dst_dir):
    if not os.path.exists(src_dir):
        raise ValueError(f'Source directory '{src_dir}' does not exist.')

    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)

    for root, _, files in os.walk(src_dir):
        for file in files:
            src_path = os.path.join(root, file)
            dst_path = os.path.join(dst_dir, file)

            if os.path.exists(dst_path):
                continue

            shutil.copy(src_path, dst_path)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Merge resource files from a source directory to a destination directory.')
    parser.add_argument('src_dir', type=str, help='the source directory containing the resource files')
    parser.add_argument('dst_dir', type=str, help='the destination directory to merge the resource files into')
    args = parser.parse_args()

    try:
        merge_res(args.src_dir, args.dst_dir)
        print('Resource files merged successfully.')
    except Exception as e:
        print(f'Error merging resource files: {e}')

使用方法:

  1. 在命令行中输入以下命令安装 argparse 模块:pip install argparse

  2. 将上述代码保存为一个 .py 文件,例如 merge_res.py

  3. 在命令行中输入以下命令运行程序:

    python merge_res.py <source_directory> <destination_directory>
    

    其中 <source_directory> 是需要合并的源目录,<destination_directory> 是合并后资源文件存放的目标目录。

    示例:python merge_res.py C:\Game\assets C:\Game\merged_assets

注意:

  • 该程序将会把源目录中的所有资源文件(包括子目录中的文件)复制到目标目录中。
  • 如果目标目录中已经存在同名文件,则不会进行复制。
  • 如果需要替换目标目录中的同名文件,可以在程序中添加相关逻辑。
Python 资源合并工具:轻松合并目录文件

原文地址: https://www.cveoy.top/t/topic/n0ff 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录