for host in platforms:
f = open(host, 'w')
f.write('define host {\n')
f.write(' host_name {}\n'.format(host))
f.write(' alias {}\n'.format(host))
f.write(' display_name {}\n'.format(host))
f.write(' address {}\n'.format(str(socket.gethostbyname(host))))
f.write(' use linux-server\n')
f.write(' register 1\n')
f.write('}\n')
Is there a better way? Is there a simple way to format all these string but only do one write method call? If the above is considered best practice that’s fine, just seems like it could be prettier some other way.
You can use named substitutions in a long, triple-quoted string.
But note that you must double your curly braces to escape them here.