Print to file
- open
We can also use the print
function to print (or write) to a file. In this case the same rules apply as printing to
standard output (automatically adding a trailing newline, inserting a space between parameters). We do this
by passing the file-handle as the value of the file
parameter of print
.
filename = 'out.txt'
with open(filename, 'w') as fh:
print("Hello", "World", file=fh)