- open
Print to file
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.
examples/files/print_to_file.py
filename = 'out.txt' with open(filename, 'w') as fh: print("Hello", "World", file=fh)