In Python 3, the print
function has an optional named parameter, file
,
which you can use for this:
>>> f = open("output.txt", "w")
>>> print("test", file=f)
>>> f.close()
You can also use the write
method on the file directly:
>>> f = open("output.txt", "w")
>>> f.write("test")
>>> f.close()