import os from subprocess import Popen main_nml = open('PART.nml', 'r') output_nml = open('PART-x-combined.nml', 'w') output_nml.close() # clear the content of the file output_nml = open('PART-x-combined.nml', 'w') prefix = '//#include ' prefix_length = len(prefix) for line in main_nml: if prefix in line: output_nml.write(line) filename_to_import = line[prefix_length:][:-1] if os.path.isfile(filename_to_import): with open(filename_to_import,'r') as file_to_import: file_to_import = open(filename_to_import, 'r') for l in file_to_import: output_nml.write(l) else: print(filename_to_import, 'not found!!!') else: output_nml.write(line) main_nml.close() output_nml.close() script_path = os.path.realpath(__file__) script_folder = os.path.dirname(script_path) compiling = Popen('compile.bat', cwd=script_folder, shell = True) print('Success! NML combined.')