How Do You Write to a File with Python?

Problem scenario
You want to store variable values in a log file.  How do you get Python to write to a log file?

Solution
Use these lines in your Python program:

var1 = 'contint'

with open('contint.log', 'a') as contint_log:
    contint_log.write("\n")  # creates a new line
    contint_log.write(var1)
    contint_log.write("Done writing variable.\n") # template text

Leave a comment

Your email address will not be published. Required fields are marked *