简体   繁体   English

显示子流程check_output的可读输出

[英]Display readable output from subprocess check_output

How can I make my output from subprocess.check_output(args, "command") readable by humans? 如何使subprocess.check_output(args, "command")输出对人类可读? I have researched this topic, and have found how to "tokenize" but not erm.. "de-tokenize" the output. 我已经研究了这个主题,并且发现了如何“标记化”而不是“ erm .. ”“反标记化”输出。 My example code below is relatively what I'm after, but to explain better, when I print the string that string = subprocess.check_output(...) feeds out, it is nearly unintelligible. 我下面的示例代码相对来说是我想要的,但是为了更好地解释,当我打印string = subprocess.check_output(...)发出的string = subprocess.check_output(...)时,它几乎是难以理解的。

Code: 码:

from subprocess import *
readOutput = check_output(
    "dir",
    shell = True)
print(readOutput)`

Output: 输出:

b' Volume in drive C has no label.\r\n Volume Serial Number is 2AAE-9786\r\n\r\n Directory of C:\\Users\\spike\\Documents\\GitHub\\GitterGUI\\example\r\n\r\n10/19/2015  06:29 PM    <DIR>          .\r\n10/19/2015  06:29 PM    <DIR>          ..\r\n10/19/2015  06:29 PM                60 batfile.bat\r\n10/19/2015  06:26 PM                 0 New Bitmap Image.bmp\r\n10/19/2015  06:26 PM                22 New Compressed (zipped) Folder.zip\r\n10/19/2015  06:26 PM    <DIR>          New folder\r\n10/19/2015  06:26 PM    <DIR>          New folder (2)\r\n10/19/2015  06:26 PM                 0 New Text Document (2).txt\r\n10/19/2015  06:26 PM                 0 New Text Document (3).txt\r\n10/19/2015  06:26 PM                 0 New Text Document.txt\r\n10/19/2015  06:27 PM                96 script.py\r\n               7 File(s)            178 bytes\r\n               4 Dir(s)  819,483,295,744 bytes free\r\n'

It should be: 它应该是:

 Volume in drive C has no label.
 Volume Serial Number is 2AAE-9786

 Directory of C:\Users\spike\Documents\GitHub\GitterGUI\example

10/19/2015  06:29 PM    <DIR>          .
10/19/2015  06:29 PM    <DIR>          ..
10/19/2015  06:29 PM                60 batfile.bat
10/19/2015  06:26 PM                 0 New Bitmap Image.bmp
10/19/2015  06:26 PM                22 New Compressed (zipped) Folder.zip
10/19/2015  06:26 PM    <DIR>          New folder
10/19/2015  06:26 PM    <DIR>          New folder (2)
10/19/2015  06:26 PM                 0 New Text Document (2).txt
10/19/2015  06:26 PM                 0 New Text Document (3).txt
10/19/2015  06:26 PM                 0 New Text Document.txt
10/19/2015  06:27 PM                96 script.py
               7 File(s)            178 bytes
               4 Dir(s)  819,481,882,624 bytes free

As you can see, my script needs serious work. 如您所见,我的脚本需要认真的工作。 Thanks in advance. 提前致谢。

Use print((readOutput).decode('utf-8')) instead of print(readOutput) 使用print((readOutput).decode('utf-8'))代替print(readOutput)

The issue was that the output to the string readOutput was not Unicode format. 问题是字符串readOutput的输出不是Unicode格式。

Edit: 编辑:

I liked the response: 我喜欢这个回应:

"Alternatively, pass universal_newlines=True to check_output to make it decode automatically." “或者,将universal_newlines=True传递给check_output以使其自动解码。”

ShadowRanger ShadowRanger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM