Strangely enough, that part of the code works good for me in Python 3.7.3 (Linux):
Python 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> stdout = sys.stdout.buffer >>> type(stdout) <class '_io.BufferedWriter'>