print in a file python code example
Example 1: print in text file python
price = 33.3
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: %s price %f" % (TotalAmount, price))
Example 2: python save output to file
with open("output.txt", "a") as f:
print("Hello StackOverflow!", file=f)
print("I have a question.", file=f)
Example 3: python print file
import sys
print("Hello Python!", file=open('output.txt','a'))
print("Not written")
sys.stdout = open('output.txt','wt')
print("Hello Python!")