change print >> stdout to sys.stdout.write()