I know this file is going to be awful, but a pattern I like is:
output = []
output.append("void myfunc() {")
output.append("    dostuff();")
output.append("}")
write("\n".join(output))

it also makes things easier to debug because you can write output to stdout or a file
then at the very least you avoid all the \ns.