print()
normally prints to the <stdout>
file stream. You can change this behavior using the file
parameter. You can access the <stdout>
stream directly from the sys
library:
>>> import sys
>>> result = sys.stdout.write('hello\n')
hello
>>> result
6
You can also change where print()
prints to:
with open('file.txt', mode='w') as file_object:
print('hello world', file=file_object)
eshivaprasad on May 17, 2020
Are there situations in which sys.stdout.write() is preferable than print()?