Why does command output on Windows return as byte format?

This is the place for queries that don't fit in any of the other categories.

Why does command output on Windows return as byte format?

Postby jxd » Thu Jun 12, 2014 7:16 pm

So I'm trying to capture the output of a command on Windows, but the output is in byte format. The version of Python is 3.3.4

Here's the most basic version of the script:

Code: Select all
import sys,subprocess

proc = subprocess.Popen(['dir'], stdout=subprocess.PIPE,shell=True)
(out,err)=proc.communicate()
print (out)


The output:
Code: Select all
b' Volume in drive C has no label.\r\n Volume Serial Number is 24AF-B71F\r\n\r\
 Directory of C:\\\r\n\r\n03/14/2014  10:55 AM    <DIR>          Admin\r\n08/23
2012  03:54 PM    <DIR>          inetpub\r\n06/12/2014  01:05 PM
34 inventory.txt\r\n04/02/2013  09:17 AM    <DIR>          jenkins\r\n12/17/201
  10:41 PM    <DIR>          kav\r\n02/04/2013  07:06 PM    <DIR>          Logs
r\n11/08/2012  07:09 PM    <DIR>          New folder\r\n10/15/2012  11:47 AM
<DIR>          oracle\r\n07/13/2009  11:20 PM    <DIR>          PerfLogs\r\n05/
6/2014  02:28 PM    <DIR>          Program Files\r\n03/20/2014  10:04 AM    <DI
>          Program Files (x86)\r\n06/12/2014  03:09 PM               846 puppet
tl.py\r\n03/20/2014  10:08 AM    <DIR>          Python33\r\n03/20/2014  10:45 A
    <DIR>          RueApps\r\n06/12/2014  03:14 PM               137 test.py\r\
05/22/2014  05:04 PM    <DIR>          tmp\r\n06/11/2014  04:44 PM    <DIR>
     Users\r\n05/16/2014  03:34 PM    <DIR>          Windows\r\n
 File(s)          1,117 bytes\r\n              15 Dir(s)  73,619,038,208 bytes


How do I capture the output as a regular string?
Last edited by Yoriz on Thu Jun 12, 2014 7:47 pm, edited 1 time in total.
Reason: First post lock.
jxd
 
Posts: 2
Joined: Thu Jun 12, 2014 7:03 pm

Re: Why does command output on Windows return as byte format

Postby micseydel » Thu Jun 12, 2014 8:16 pm

You can call str() on it to turn the bytes into a regular (unicode, in Python 3) string. If that doesn't answer your question, you might want to tell us why you want a "regular" string.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1505
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Why does command output on Windows return as byte format

Postby jxd » Fri Jun 13, 2014 2:31 pm

Well actually str() didn't work, I had to use

Code: Select all
bytes.decode(out)


As to why? It seems to return string on *nix, so this is an annoyance when I need the script to work the same on both platforms.
jxd
 
Posts: 2
Joined: Thu Jun 12, 2014 7:03 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Mekire, micseydel, Nix0101, silas247 and 6 guests