title: python 3 subprocess error in bytes
body: <p>Very good, I have a little problem with the output of the thread, I get in unicode or I think and not let me convert it to utf-8, this is the code:</p>
  
  <pre><code>import subprocess,sys,time
  
  string = b'dir'
  process = subprocess.Popen('cmd.exe', shell=True,cwd="C:\\",stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)
  process.stdin.write(string)
  o,e=process.communicate()
  process.wait()
  process.stdin.close()
  print (o.encode('utf-8'))
  </code></pre>
  
  <p>I jump the following error:</p>
  
  <pre><code>**Traceback (most recent call last):
    File "C:\Documents and Settings\francisco\Escritorio\k.py", line 12, in &lt;module&gt;
      print (o.encode(utf-8))
  AttributeError: 'bytes' object has no attribute 'encode'**
  </code></pre>
  
  <p>if I print leaving the print and if you let me:</p>
  
  <pre><code>print(o)
  </code></pre>
  
  <p>but it prints the following:</p>
  
  <pre><code>**b'Microsoft Windows XP [Versi\xa2n 5.1.2600]\r\n(C) Copyright 1985-2001 Microsoft Corp.\r\n\r\nC:\\&gt;\xa8M\xa0s? '**
  </code></pre>
  
  <p>and if I change these two lines:</p>
  
  <pre><code>string = bytes('dir',encoding="utf-8") 
  print (n[0].decode("latin"))
  </code></pre>
  
  <p>I print only part of the output</p>
  
  <p>that fails?</p>
  
  <hr>
  
  <p>I've solved this way:</p>
  
  <pre><code>process.stdin.write("dir\n".encode())
  o,e=process.communicate()
  print (o.decode("utf-8"))
  </code></pre>
  
  <p>but I get error:</p>
  
  <p><strong>Traceback (most recent call last):
    File "C:\Documents and Settings\francisco\Escritorio\k.py", line 6, in 
      print (o.decode("utf-8"))
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 103: invalid start byte</strong></p>
  
  <p>I just print it like this:</p>
  
  <pre><code>print (o.decode("latin"))
  </code></pre>
  
  <p>in latin, I can correct this error and print it in utf-8?</p>