python获取控制台输出_Python | 用Python获取Windows控制台输出
1 os模塊實現
import os
cmd = 'ping baidu.com'
r = os.popen(cmd)
for line in r.readlines():
print (line)
以上代碼利用os.popen()指令獲取了控制臺的輸出內容。
結果示例:
正在 Ping baidu.com [123.125.115.110] 具有 32 字節的數據:
來自 123.125.115.110 的回復: 字節=32 時間=50ms TTL=44
來自 123.125.115.110 的回復: 字節=32 時間=51ms TTL=44
來自 123.125.115.110 的回復: 字節=32 時間=50ms TTL=44
來自 123.125.115.110 的回復: 字節=32 時間=52ms TTL=44
123.125.115.110 的 Ping 統計信息:
數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失),
往返行程的估計時間(以毫秒為單位):
最短 = 50ms,最長 = 52ms,平均 = 50ms
辨析os.system與os.popen函數
兩個函數均執行參數中的系統指令
os.system指令返回值為系統的退出狀態碼
os.popen指令返回值為控制臺的輸出內容
2 subprocess模塊實現
PEP324標準中,提出了用subprocess模塊取代os.system和os.popen等函數的功能。在實踐中,subprocess模塊也比os.popen等函數有更好的表現,特別是在多線程開發中。
該模塊中最重要的類是Popen類
class Popen(args, bufsize=0, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False, shell=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0):
常用的參數:
args should be a string, or a sequence of program arguments. The program to execute is normally the first item in the args sequence or string, but can be explicitly set by using the executable argument.
stdin, stdout and stderr specify the executed programs' standard input, standard output and standard error file handles, respectively. Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None. PIPE indicates that a new pipe to the child should be created. With None, no redirection will occur; the child's file handles will be inherited from the parent. Additionally, stderr can be STDOUT, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.
If shell is true, the specified command will be executed through the shell.
If cwd is not None, the current directory will be changed to cwd before the child is executed. ( cwd means current working directory)
If env is not None, it defines the environment variables for the new process.
常用的方法:
poll(): Check if child process has terminated. Returns returncode attribute.
wait(): Wait for child process to terminate. Returns returncode attribute.
communicate(input=None)
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional stdin argument should be a string to be sent to the child process, or None, if no data should be sent to the child.
communicate() returns a tuple (stdout, stderr).
Note: The data read is buffered in memory, so do not use this method if the data size is large or unlimited.
如果用subprocess模塊實現第一節的功能,代碼如下:
cmd = ['ping','baidu.com']
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
p.stdout.read().decode('gbk')
輸出:
'\r\n正在 Ping baidu.com [123.125.115.110] 具有 32 字節的數據:\r\n來自 123.125.115.110 的回復: 字節=32 時間=50ms TTL=44\r\n來自 123.125.115.110 的回復: 字節=32 時間=51ms TTL=44\r\n來自 123.125.115.110 的回復: 字節=32 時間=50ms TTL=44\r\n來自 123.125.115.110 的回復: 字節=32 時間=51ms TTL=44\r\n\r\n123.125.115.110 的 Ping 統計信息:\r\n 數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失),\r\n往返行程的估計時間(以毫秒為單位):\r\n 最短 = 50ms,最長 = 51ms,平均 = 50ms\r\n'
subprocess輸出的轉碼問題
在上面的示例代碼中,我們注意到,代碼最后一行使用了GBK格式解碼。我們是怎樣確定解碼的格式的呢?
subprocess的輸出格式與命令行的輸出格式一致,而命令行的輸出格式可以按照如下方式查詢:
在命令行執行chcp指令,得到活動頁代碼。比如,在我的電腦中執行結果如下所示。
C:\Users\Administrator>chcp
活動代碼頁: 936
然后依照下表查詢可得字符集編碼。
MS-DOS為以下國家和語言提供字符集:
代碼頁描述
1258 越南語
1257 波羅的語
1256 阿拉伯語
1255 希伯來語
1254 土耳其語
1253 希臘語
1252 拉丁 1 字符 (ANSI)
1251 西里爾語
1250 中歐語言
950 繁體中文
949 朝鮮語
936 簡體中文(默認) GBK
932 日語
874 泰國語
850 多語種 (MS-DOS Latin1)
437 MS-DOS 美國英語
65001 UTF-8
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python获取控制台输出_Python | 用Python获取Windows控制台输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python迭代法求解非线性方程_荐【数
- 下一篇: 阿里云mysql写入性能_插入mysql