[英]How to get the output of subprocess.check_output() python module?
我正在嘗試從python中的命令提示符(CMD - Windows)獲取信息,使用模塊子進程,如下所示:
ipconfig = subprocess.check_output("ipconfig")
print(ipconfig)
結果是:
b'\r\nWindows IP Configuration\r\n\r\n\r\nEthernet adapter Local Area Connect:\r\n\r\n Connection-specific DNS Suffix . : XX.XXX\r\n IPv4 address. . . . . . . . . . . : XXXXXXXXX\r\n Subnet Mask . . . . . . . . . . . : XXXXXXXXXX\r\n Default Gateway . . . . . . . . . : XXXXXX\r\n'
我已經閱讀了關於module subprocess
的文檔,但我找不到任何適合我的問題的文檔。
我需要一個很好的格式化字符串中的這些信息...不是那樣的,我該怎么辦?
(我認為這里的問題是一個字符串格式,但是如果你有一個不需要字符串格式就可以得到一個好輸出的技巧,我很感激)
我知道我可以使用套接字模塊獲取IP地址,但我只是舉個例子。
您正在打印bytes
字符串值, print()
使用repr()
將其轉換為unicode字符串。 您想要將值解碼為unicode:
import sys
print(ipconfig.decode(sys.stdout.encoding))
這使用控制台的編碼來解釋命令輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.