Python save to file loop

137 views Asked by At

I need some help to create a loop for my program. My problem is that I have to run the script twice, before the file/output registers any changes done to my configuration.

Programming is not my strongest skill - hope someone can be of assistance

UPDATED code:

try:
    rescue = cu.rescue(action="get", format="text")
    if rescue is None:
        print ("No existing rescue configuration.")
        print ("Saving rescue configuration.")
        cu.rescue(action="save")    
    else:
        print ("Rescue configuration exist:")
        print ("Updating rescue configuration")
        cu.rescue(action="save")
        print (rescue)
except Exception as err:
    print (err)

with open("Rescue Config.config", "w") as text_file:
text_file.write(rescue)
1

There are 1 answers

0
vencaslac On

indentation matters in python, change the last few lines of your code to this

with open("Rescue Config.config", "w") as text_file:
     text_file.write(rescue)

no need to close the file, this happens automatically when using the with statement