python调用shell命令
在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
python调用shell命令相关推荐
- python调用shell命令-Python调用shell命令常用方法(4种)
方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256表示未找到,该方法适用于shell命令 ...
- python调用shell命令-用Python调用Shell命令
Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如 ...
- python调用shell命令-在Python中执行shell命令的6种方法,你都知道吗?
原标题:在Python中执行shell命令的6种方法,你都知道吗? Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库.今天我们就讲解 ...
- python调用shell命令-「Python」6种python中执行shell命令方法
用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...
- python 执行shell_用Python调用Shell命令
Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如 ...
- python 调用shell命令的方法
转载: https://www.cnblogs.com/thinker-lj/p/3860123.html https://www.cnblogs.com/wenwangt/p/4897961.htm ...
- python 调用linux命令-Python调用shell命令常用方法
Python调用shell指令 方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256表示未 ...
- python调用shell命令-Python怎么运行shell脚本
Python作为一门脚本语言,有时候需要与shell命令交互式使用,在Python中提供了很多的方法可以调用并执行shell脚本,本文介绍几个简单的方法. Python怎么运行shell脚本 一.os ...
- python调用shell命令-python调用shell命令小结
在写python脚本的时候,经常需要调用系统命令,常用的python调用系统命令的方法主要有subprocess.call和os.popen.默认情况下subprocess.call的方法结果是返回值 ...
最新文章
- 浅析枚举类型(Enumerated types)
- hMailServer C#API
- C# 入门之 Hello World
- 这次使用一个最舒服的姿势插入HttpClient拦截器技能点
- java学习_File属性处理
- html创建文件域的代码,word如何插入域代码
- 25. PE结构-PE详解之资源
- oracle extract函数笔记
- 《KyLin学习理解》-01-KyLin麒麟的简介及其思想
- 2017 成为更好的自己需要做的事情
- win11打开应用被管理员阻止怎么办 window11管理员已阻止你运行此应用的解决方法
- java .class 实例对象_Java产生Class类的三种实例化对象的方法
- [解决方案]word文档无法存放过长过大的visio图片
- 基础篇:4.1)规范化:3d工程图纸出图步骤详解
- Facebook攻略--注册流程
- 基于Lua的游戏服务端框架简介
- 上海亚商投顾:创业板指高开高走 ChatGPT概念卷土重来
- 81、前后端等上下游岗位配合的思考和参考工作方法,望文知意,约定优于沟通
- 颜色 /About Color --图形学的B面(二)
- 求和计算机教案,面试真题gt;小学信息技术《自动求和》教学设计
热门文章
- flume系列之:监控JMX reporter
- 一笔画 java_NYOJ42 一笔画问题
- Citavi——令人激动的文献管理工具
- 如何把拍摄视频中多余的人或物去除?
- 全球多地爆发!传染性极强!鄂尔多斯人近期外出一定要注意…
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
- c语言素数筛法与分解素因数,质因数分解及代码:
- apmserv mysql5.1启动失败_APMServ Apache启动失败及MySQL5.1启动失败,请检查相关配置 解...
- 利用 ffmpeg swr_convert重采样
- 结构体、共用体、位操作和枚举类型