import sys
# only this print call will write in the file
print("Hello Python!", file=open('output.txt','a'))
# not this one (std output)
print("Not written")
# any further print will be done in the file
sys.stdout = open('output.txt','wt')
print("Hello Python!")
#you do not have to create a text file in advance, this program will create the file if it does not exist.
whatever = ('this is what will be printed to your file')
sourceFile = open('demo.txt', 'w')
print(whatever, file = sourceFile)
sourceFile.close()
print("Hello stackoverflow!", file=open("output.txt", "a"))
print("I have a question.", file=open("output.txt", "a"))
# Print to the text file
file = open('log.txt', 'w')
print('This is a sample print statement', file = file)
print('Hello World', file = file)
file.close()
print('Hi', file=open('output.txt', 'a'))
print('Hello from AskPython', file=open('output.txt', 'a'))
print('exit', file=open('output.txt', 'a'))