print to a file python code example
Example 1: python print to file
import sys
print("Hello Python!", file=open('output.txt','a'))
print("Not written")
sys.stdout = open('output.txt','wt')
print("Hello Python!")
Example 2: python write to file
with open(filename,"w") as f:
f.write('Hello World')
Example 3: 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 4: python file open
file = open('C:\Users\yourname\files\file.txt','r')
text = file.read()
file = open('C:\Users\yourname\files\file.txt','w')
file.write('This is a typical string')
file.close()
Example 5: print output python to file
print("Hello stackoverflow!", file=open("output.txt", "a"))
print("I have a question.", file=open("output.txt", "a"))
Example 6: how to write to an output file in pytion
outputFile = open("fileName", "w")
outputFile.write(str)
outputFile.close()