print(open('log txt'read() these extended forms of print are also commonly used to print error messages to the standard error streamavailable to your script as the preopened file object sys stderr you can either use its file write methods and format the output manuallyor print with redirection syntaximport sys sys stderr write(('bad! '\ 'bad!bad!bad!bad!bad!bad!bad!badprint('bad! file=sys stderrbad!bad!bad!bad!bad!bad!bad!badin xprint >sys stderr'bad! now that you know all about print redirectionsthe equivalence between printing and file write methods should be fairly obvious the following interaction prints both ways in xthen redirects the output to an external file to verify that the same text is printedx print(xy import sys sys stdout write(str(xstr( '\ ' print(xyfile=open('temp '' ')printthe easy way printthe hard way redirect text to file open('temp '' 'write(str(xstr( '\ 'send to file manually print(open('temp ''rb'read()binary mode for bytes ' \ \nprint(open('temp ''rb'read() ' \ \nas you can seeunless you happen to enjoy typingprint operations are usually the best option for displaying text for another example of the equivalence between prints and file writeswatch for print function emulation example in it uses this code pattern to provide general print function equivalent for use in python version-neutral printing finallyif you need your prints to work on both python linesyou have some options this is true whether you're writing code that strives for compatibilityor code that aims to support too to converter for oneyou can code print statements and let ' to conversion script translate them to function calls automatically see the python manuals for more details assignmentsexpressionsand prints