A test file named == a.py ==
import os
if __name__ == '__main__':
for x in range(0,1000):
try:
os.sys.stdout.buffer.write(b"hello\r\n")
os.sys.stdout.buffer.flush()
except OSError:
pass
It is ok when it runs alone. But there is problem in following case:
- Run python a.py | more
- Press ctrl+c to exit
Then I got Exception OSError: OSError(22, 'Invalid argument') in <_io.TextIOWrapper name='' mode='w' encoding='cp936'> ignored
I have catch the OSError already around the os.sys.stdout.buffer operations, but it still goes wrong.
If i don't use os.sys.stdout.buffer.flush, then there's no error message. If I just use print(), everything is OK.
So how to write bytes to the stdout correctly?
I am using Windows 7 64 bit and Python 3.3.0 32 bit.