How can I write a for loop that writes a code template to file in python 3.7

347 Views Asked by At

I was wondering if there is a way to write a template to file inside of a for loop. The code below is an example of what I have written so far. The variable inside of the open function equals 'python.py'.

with open(filename, 'w') as fout:
    x = 0
    for x in range(5):
        fout.write(""" def function_number{}(): \n
""".format(x))

Here is what I am expecting to happen

def function_number0():

def function_number1():

def function_number2():

def function_number3():

def function_number4():

Here is the output I receive

def function_number0():

            def function_number1():

            def function_number2():

            def function_number3():

            def function_number4():

How can I restructure my code to get the desired output?

0

There are 0 best solutions below