print() function with encoding= and errors= parameters?

Random832 random832 at fastmail.com
Wed Aug 3 10:05:12 EDT 2016


On Wed, Aug 3, 2016, at 08:29, Malcolm Greene wrote:
> Looking for a way to use the Python 3 print() function with encoding and
> errors parameters.
> 
> Are there any concerns with closing and re-opening sys.stdout so
> sys.stdout has a specific encoding and errors behavior? Would this break
> other standard libraries that depend on sys.stdout being configured a
> specific way?

You could duplicate stdout [open(os.dup(sys.stdout.fileno()), ...)]

You could make an IO class which sends bytes output to sys.stdout.buffer
but won't close it when closed (you know, it'd be nice to be able to
have "not owned underlying stream" as an option of the standard IO
classes)

If your output isn't going to be long, you could print everything to a
StringIO and then write to sys.stdout.buffer at the end.



More information about the Python-list mailing list