在python程序中调用shell命令
1. os.system(command)
此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出状态,如果command有执行内容,会在标准输出显示。这实际上是使用C标准库函数system()实现的。
缺点:这个函数在执行command命令时需要重新打开一个终端,并且无法保存command命令的执行结果。
实例:os.system('ls -l *')
2. os.popen(command,mode)
打开一个与command进程之间的管道。这个函数的返回值是一个文件对象,可以读或者写(由mode决定,mode默认是’r')。如果mode为’r',可以使用此函数的返回值调用read()来获取command命令的执行结果。
os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容。实际使用时视需求情况而选择。
实例:tmp = os.popen('ls -l *').readlines()
3. commands.getstatusoutput(command)
使用os.popen()函数执行command命令并返回一个元组(status,output),分别表示command命令执行的返回状态和执行结果。对command的执行实际上是按照{command;} 2>&1的方式,所以output中包含控制台输出信息或者错误信息。output中不包含尾部的换行符。
4. subprocess模块
此模块在python2.4中初次亮相,其中集中了关于进程的诸多操作,其中的call()完全替代了system(),而popen()被更为丰富的Popen类替代;
总结:python提供了十分完善的调用shell命令的功能,在实战中,我碰到的问题,有system和popen基本可全部搞定;
引自:python调用shell命令的方法 http://www.cnblogs.com/thinker-lj/p/3860123.html
举例: 在windows平台上的
>>> import os >>> os.system("tasklist") 0
现象:黑色的shell闪了一下就退出关闭了,python输出结果为0
>>> os.popen("tasklist") <open file 'tasklist', mode 'r' at 0x02C40C28>
现象:输出上面字符串
>>> l = os.popen("tasklist") >>> l <open file 'tasklist', mode 'r' at 0x02C40E38> >>> tmp = os.popen('tasklist').readlines() >>> tmp
输出结果:
['\n', '\xd3\xb3\xcf\xf1\xc3\xfb\xb3\xc6 PID \xbb\xe1\xbb\xb0\xc3\xfb \xbb\xe1\xbb\xb0# \xc4\xda\xb4\xe6\xca\xb9\xd3\xc3 \n', '========================= ======== ================ =========== ============\n', 'System Idle Process 0 Services 0 24 K\n', 'system 4 Services 0 368 K\n', 'smss.exe 328 Services 0 1,232 K\n', 'csrss.exe 484 Services 0 5,748 K\n', 'wininit.exe 744 Services 0 5,336 K\n', 'csrss.exe 768 Console 1 76,116 K\n', 'services.exe 812 Services 0 10,964 K\n', 'winlogon.exe 844 Console 1 28,696 K\n', 'lsass.exe 852 Services 0 14,180 K\n', 'lsm.exe 860 Services 0 4,544 K\n', 'svchost.exe 980 Services 0 10,080 K\n', 'svchost.exe 376 Services 0 8,328 K\n', 'svchost.exe 564 Services 0 20,040 K\n', 'svchost.exe 620 Services 0 219,028 K\n', 'svchost.exe 636 Services 0 37,480 K\n', 'svchost.exe 1028 Services 0 12,580 K\n', 'igfxCUIService.exe 1080 Services 0 7,708 K\n', 'RtkAudioService64.exe 1144 Services 0 5,572 K\n', 'RAVBg64.exe 1232 Console 1 12,532 K\n', 'RAVBg64.exe 1240 Console 1 11,728 K\n', 'ZhuDongFangYu.exe 1264 Services 0 45,404 K\n', 'svchost.exe 1328 Services 0 18,032 K\n', 'spoolsv.exe 1668 Services 0 15,384 K\n', 'svchost.exe 1700 Services 0 19,760 K\n', 'AlibabaProtect.exe 1948 Services 0 33,248 K\n', 'AliWorkbenchSafe.exe 1984 Services 0 25,244 K\n', 'AppleMobileDeviceService. 2008 Services 0 11,876 K\n', 'mDNSResponder.exe 1216 Services 0 5,872 K\n', 'sqlservr.exe 1912 Services 0 3,720 K\n', 'mysqld.exe 2152 Services 0 177,364 K\n', 'qcmtusvc.exe 2196 Services 0 6,448 K\n', 'QQProtect.exe 2236 Services 0 28,048 K\n', 'sqlwriter.exe 2272 Services 0 6,852 K\n', 'svchost.exe 2292 Services 0 5,712 K\n', 'vmnat.exe 2344 Services 0 5,164 K\n', 'taskhost.exe 2680 Console 1 12,308 K\n', 'dwm.exe 2736 Console 1 42,200 K\n', 'explorer.exe 2776 Console 1 95,128 K\n', 'WmiPrvSE.exe 2536 Services 0 14,444 K\n', 'vmware-authd.exe 2720 Services 0 17,868 K\n', 'vmnetdhcp.exe 3028 Services 0 10,512 K\n', 'vmware-usbarbitrator64.ex 3092 Services 0 8,112 K\n', 'RtkNGUI64.exe 3308 Console 1 16,420 K\n', 'TSVNCache.exe 3468 Console 1 12,980 K\n', 'RAVBg64.exe 3576 Console 1 14,392 K\n', 'RAVBg64.exe 3584 Console 1 12,784 K\n', '360tray.exe 3832 Console 1 44,948 K\n', '91AssistProxy.exe 3860 Console 1 7,560 K\n', 'lmgrd.exe 4040 Console 1 6,240 K\n', 'arcd.exe 3172 Console 1 6,200 K\n', 'TXPlatform.exe 4808 Console 1 2,508 K\n', 'AliworkbenchSafeUI.exe 4912 Console 1 16,616 K\n', 'svchost.exe 3924 Services 0 6,276 K\n', 'svchost.exe 4496 Services 0 40,048 K\n', 'igfxEM.exe 3188 Console 1 13,488 K\n', 'igfxHK.exe 4412 Console 1 11,496 K\n', 'igfxTray.exe 4508 Console 1 9,300 K\n', 'SoftMgrLite.exe 1396 Console 1 16,300 K\n', 'chrome.exe 4184 Console 1 155,048 K\n', 'chrome.exe 5168 Console 1 7,064 K\n', 'chrome.exe 5480 Console 1 10,396 K\n', 'chrome.exe 5612 Console 1 89,440 K\n', 'chrome.exe 5772 Console 1 59,028 K\n', 'chrome.exe 5784 Console 1 47,276 K\n', 'chrome.exe 5796 Console 1 27,476 K\n', 'chrome.exe 5812 Console 1 63,732 K\n', 'cmd.exe 5368 Console 1 3,768 K\n', 'conhost.exe 5404 Console 1 8,736 K\n', 'chromeht.exe 2452 Console 1 5,624 K\n', 'QQ.exe 5844 Console 1 386,860 K\n', 'chrome.exe 4520 Console 1 128,552 K\n', 'chrome.exe 6132 Console 1 25,400 K\n', 'QQExternal.exe 3192 Console 1 49,604 K\n', 'QQExternal.exe 4172 Console 1 36,160 K\n', 'wwbizsrv.exe 6592 Services 0 7,496 K\n', 'PanDownload.exe 6976 Console 1 31,472 K\n', 'aria2c.exe 688 Console 1 11,384 K\n', 'conhost.exe 4368 Console 1 8,604 K\n', 'chrome.exe 5556 Console 1 144,956 K\n', 'wps.exe 4232 Console 1 213,068 K\n', 'wpscloudsvr.exe 5408 Console 1 10,080 K\n', 'wpscenter.exe 6896 Console 1 9,312 K\n', 'wpscenter.exe 6884 Console 1 2,792 K\n', 'WmiPrvSE.exe 6988 Services 0 26,532 K\n', 'WmiPrvSE.exe 7184 Services 0 10,124 K\n', 'GeePlayer.exe 7908 Console 1 117,604 K\n', 'pythonw.exe 1684 Console 1 27,524 K\n', 'pythonw.exe 1016 Console 1 14,304 K\n', 'chrome.exe 7512 Console 1 262,116 K\n', 'cmd.exe 7624 Console 1 3,248 K\n', 'conhost.exe 5232 Console 1 15,168 K\n', 'audiodg.exe 4356 Services 0 21,476 K\n', 'cmd.exe 5336 Console 1 3,724 K\n', 'conhost.exe 4484 Console 1 8,696 K\n', 'tasklist.exe 7336 Console 1 5,996 K\n', 'cmd.exe 5636 Console 1 3,720 K\n', 'conhost.exe 7044 Console 1 8,812 K\n', 'tasklist.exe 6524 Console 1 5,740 K\n']
现象输出了字符串文本,但因字符集问题,没有显示中文
取最后一行
>>> s = tmp[-1] >>> s
'tasklist.exe 6524 Console 1 5,740 K\n'
来源:https://www.cnblogs.com/smulngy/p/8861453.html