text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: %s" % TotalAmount)
text_file.close()
Si usa un administrador de contexto, el archivo se cierra automáticamente.
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: %s" % TotalAmount)
Si está utilizando Python2.6 o superior, se prefiere usar str.format()
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: {0}".format(TotalAmount))
Para python2.7 y superior puede usar en {}
lugar de{0}
En Python3, hay un file
parámetro opcional para la print
función
with open("Output.txt", "w") as text_file:
print("Purchase Amount: {}".format(TotalAmount), file=text_file)
Python3.6 introdujo f-strings para otra alternativa
with open("Output.txt", "w") as text_file:
print(f"Purchase Amount: {TotalAmount}", file=text_file)