diff --git a/autopep8.py b/autopep8.py
index <HASH>..<HASH> 100755
--- a/autopep8.py
+++ b/autopep8.py
@@ -1840,10 +1840,9 @@ def main():
         assert not opts.recursive
         filenames = args[:1]
 
-    if sys.version_info[0] >= 3:
-        output = sys.stdout
-    else:
-        output = codecs.getwriter('utf-8')(sys.stdout)
+    output = codecs.getwriter('utf-8')(sys.stdout.buffer
+                                       if sys.version_info[0] >= 3
+                                       else sys.stdout)
 
     output = LineEndingWrapper(output)
Use codecs.getwriter() for Python 3

This makes things work the same as in Python 2, where we were always
outputting as Unicode.