【发布时间】:2015-10-20 01:54:04
【问题描述】:
如何使 subprocess.check_output(args, "command") 的输出可供人类阅读?我研究了这个主题,并找到了如何“标记化”而不是erm..“去标记化”输出。
我下面的示例代码是我所追求的,但为了更好地解释,当我打印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
如您所见,我的脚本需要认真工作。提前致谢。
【问题讨论】:
标签: python python-3.x subprocess output tokenize