using sys.stdout.write for Python 3.X support