显示子进程的可读输出 check_output
Display readable output from subprocess check_output
如何使 subprocess.check_output(args, "command")
的输出可供人类阅读?我研究了这个主题,并找到了如何 "tokenize" 而不是 erm.. "de-tokenize" 输出。
我下面的示例代码相对来说是我所追求的,但为了更好地解释,当我打印 string = subprocess.check_output(...)
输出的字符串时,它几乎无法理解。
代码:
from subprocess import *
readOutput = check_output(
"dir",
shell = True)
print(readOutput)`
输出:
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'
应该是:
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
如您所见,我的脚本需要认真工作。提前致谢。
使用print((readOutput).decode('utf-8'))
而不是 print(readOutput)
问题是字符串 readOutput
的输出不是 Unicode 格式。
编辑:
我喜欢这个回复:
"Alternatively, pass universal_newlines=True
to check_output
to make it decode automatically."
如何使 subprocess.check_output(args, "command")
的输出可供人类阅读?我研究了这个主题,并找到了如何 "tokenize" 而不是 erm.. "de-tokenize" 输出。
我下面的示例代码相对来说是我所追求的,但为了更好地解释,当我打印 string = subprocess.check_output(...)
输出的字符串时,它几乎无法理解。
代码:
from subprocess import *
readOutput = check_output(
"dir",
shell = True)
print(readOutput)`
输出:
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'
应该是:
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
如您所见,我的脚本需要认真工作。提前致谢。
使用print((readOutput).decode('utf-8'))
而不是 print(readOutput)
问题是字符串 readOutput
的输出不是 Unicode 格式。
编辑:
我喜欢这个回复:
"Alternatively, pass
universal_newlines=True
tocheck_output
to make it decode automatically."