For template creation I use jinja :
from jinja2 import FileSystemLoader, Template
# Function creating from template files.
def write_file_from_template(template_path, output_name, template_variables, output_directory):
template_read = open(template_path).read()
template = Template(template_read)
rendered = template.render(template_variables)
output_path = os.path.join(output_directory, output_name)
output_file = open(output_path, 'w+')
output_file.write(rendered)
output_file.close()
print('Created file at %s' % output_path)
return output_path
journal_output = write_file_from_template(
template_path=template_path,
output_name=output_name,
template_variables={'file_output':file_output,
'step_size':step_size,
'time_steps':time_steps},
output_directory=output_directory)
With a file named file.extension.TEMPLATE:
# This is a new file :
{{ file_output }}
# The step size is :
{{ step_size }}
# The time steps are :
{{ time_steps }}
You may need to modify it a little bit, but the major things are there.
string.Template. template string can be stored in a simple text file and can be written into file format of our choice after substitution.