python os.popenn的返回对象没有

/ruiyqinrui
Python执行系统命令的方法 os.system(),os.popen(),commands
最近在做那个测试框架的时候发现 Python 的另一个获得系统执行命令的返回值和输出的类。
最开始的时候用 Python 学会了 os.system() 这个方法是很多比如 C,Perl 相似的。
os.system('cat /proc/cpuinfo')
但是这样是无法获得到输出和返回值的,继续 Google,之后学会了 os.popen()。
output&=&os.popen('cat /proc/cpuinfo') print&output.read()
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read()
的操作可以看到执行的输出。但是怎么读取程序执行的返回值呢,当然咯继续请教伟大的 Google(联想到像我这样的人工作如果离开了
Google,不是成了废物。。。Baidu 忽视)。Google 给我指向了&。 这样通过 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。
(status,&output)&=&commands.getstatusoutput('cat /proc/cpuinfo') print&status,&output
Python Document 中给的一个例子,很清楚的给出了各方法的返回。
&&&&import&commands &&&&commands.getstatusoutput('ls /bin/ls') (0,&'/bin/ls') &&&&commands.getstatusoutput('cat /bin/junk') (256,&'cat: /bin/junk: No such file or directory') &&&&commands.getstatusoutput('/bin/junk') (256,&'sh: /bin/junk: not found') &&&&commands.getoutput('ls /bin/ls') '/bin/ls' &&&&commands.getstatus('/bin/ls') '-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
/ruiyqinruipython popen执行系统命令并获取返回值
我的图书馆
python popen执行系统命令并获取返回值
示例程序如下,说明:1. &用communicate方法进行交互,直接读取p.stdout有可能数据还没刷新,会读取不到2. &windows shell参数表明是否使用windows bat作为执行的环境,因此只有在执行windows系统命令如dir,copy时才必须将此参数设置为True,其他地方True与False执行结果没区别&3. &universal_newlines参数表明输入、输出是否采用文本解析4. &comnunicate的input参数是用于手式输入参数的,如果是命令行参数参数直接在程序后面加就可以5. &没有了import subprocess& &&proc = subprocess.Popen("E:\\__Projects\\c++\\test\\Debug\\test.exe", stdin = subprocess.PIPE,&stdout = subprocess.PIPE, stderr = subprocess.PIPE,universal_newlines=True, shell = False) &&straa = '3\n'strbb = '4\n'proc.stdin.write(straa)&proc.stdin.write(strbb) &&# p.stdin.close()try:& & outs, errs = municate(timeout=15)& & # print(proc.stdout.read())& & print(outs)& &&except&TimeoutExpired&as e:& & proc.kill()& & outs, errs = municate()
TA的最新馆藏温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
很酷不拉风会死~~
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(3821)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'OS模块详解',
blogAbstract:'
简明教程:
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}python 系统调用os.popen无法进行异常捕获
& & & output = os.popen('du -s %s' % path +"| awk &'{print $1}'") & & & & & &&
& & & size = output.readline().strip('\n') & & & & & &&
except Exception,e:
& & & print e
代码如上,发现path指定的路径不存在时,该异常并没有被捕获。
请问除了自定义异常,有办法捕获到该异常吗??
读取标准输出,自己处理。
http://www.oschina.net/question/07

我要回帖

更多关于 python的os.popen 的文章

 

随机推荐