I"m trying to make a simple text progress bar in windows console and also display utf8 characters.
The problem isn't that the unicode characters won't display, they will. It's that in order to make the unicode characters display I used a class to tell sys.stdout what to do. This has interfered with the normal flush() function.
How can I get this flush() funcionality back in the console and still use this unicode class?
#coding=<utf8>
import sys, os
#make windows console unicode friendly
if sys.platform == "win32":
os.popen('chcp 65001')
class UniStream(object):
__slots__= "fileno", "softspace",
def __init__(self, fileobject):
self.fileno= fileobject.fileno()
self.softspace= False
def write(self, text):
if isinstance(text, unicode):
os.write(self.fileno, text.encode("utf_8"))
else:
os.write(self.fileno, text)
def flush(self):
self.flush()
sys.stdout = UniStream(sys.stdout)
sys.stderr = UniStream(sys.stderr)
def progress(num):
sys.stdout.write("\r"+str(num)+"% τοις εκατό...")
sys.stdout.flush()
for i in xrange(2000):
progress(i)
x = raw_input('done')