One solution could be to open a file once and then iterate inside the functionwhile the file is open:

temperatures=[10,-20,-289,100]
 
def writer(temperatures):
    with open("temps.txt", 'w') as file:
        for c in temperatures:
            if c>-273.15:
                f=c*9/5+32
                file.write(str(f)+"\n")

writer(temperatures)#Here we're calling the function, otherwise no output will be generated

Or even better, you could treat both the temperature input and the file path as function arguments:

temperatures=[10,-20,-289,100]
 
def writer(temperatures, filepath):
    with open(filepath, 'w') as file:
        for c in temperatures:
            if c>-273.15:
                f=c*9/5+32
                file.write(str(f)+"\n")

writer(temperatures, "temps.txt")#Here we're calling the function, otherwise no output will be generated

In real life you may want to build a website with Python so you connectthe function callwriter()with a button on the website. The user enters some values in a text box, and types in a file name and thenpresses the button. The button executes the writer()function by using the input that user entered in the text box as input values for the function. Once the button is pressed,the generatedfile downloads on the user's browser.

Sure, that's a bit of a demanding project to do but Iwill take you step by step in later sections on how to do such web applications.