How can I print to a file instead of standard output?
22.9. The print() function
In Python 3, print is a function, not a statement. To make it easier to convert your programs to the new syntax, first use this import statement (introduced in Python 2.6):
from future import print_function Here is the interface to this function:
print(*args, sep=' ', end='\n', file=None) args One or more positional arguments whose values are to be printed.
sep By default, consecutive values are separated by one space. You may specify a different separator string using this keyword argument.
end By default, a newline ("\n") is written after the last value in args. You may use this keywoard argument to specify a different line terminator, or no terminator at all.
file Output normally goes to the standard output stream (sys.stdout). To divert the output to another writeable file, use this keyword argument.
Here's an example. Suppose you are writing three strings named clan, moiety, and distro to a writeable file named spreader, and you want to separate the fields with tab ("\t") characters, and use ASCII CR, Carriage Return ("\r"), as the line terminator. Your call to the print() function would go something like this:
print(clan, moiety, distro, file=spreader, end='\r', sep='\t')
I found this in a Python 2.7 reference guide. I tried this code but it didn't work. I got the message: print('clan', 'moiety', 'distro', file=spreader, end='\r', sep='\t') AttributeError: 'str' object has no attribute 'write'
How can I print to a file instead of standard output?
Thank you Eli. It solved my problem