1# Copyright 2016 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import os 6 7 8def MergeFiles(dest_file, source_files): 9 """Merge list of files into single destination file. 10 11 Args: 12 dest_file: File to be written to. 13 source_files: List of files to be merged. Will be merged in the order they 14 appear in the list. 15 """ 16 if not os.path.exists(os.path.dirname(dest_file)): 17 os.makedirs(os.path.dirname(dest_file)) 18 try: 19 with open(dest_file, 'w') as dest_f: 20 for source_file in source_files: 21 with open(source_file, 'r') as source_f: 22 dest_f.write(source_f.read()) 23 except Exception as e: # pylint: disable=broad-except 24 # Something went wrong when creating dest_file. Cleaning up. 25 try: 26 os.remove(dest_file) 27 except OSError: 28 pass 29 raise e 30