您的位置:首页 > 编程语言 > Python开发

python 如何判断调用系统命令是否执行成功

2015-10-28 02:42 911 查看
首先我们要知道如何调用系统命令:

>>> os.system('ls')
anaconda-ks.cfg  install.log.syslog  模板  图片  下载  桌面
install.log         公共的                     视频  文档  音乐
0
>>>
>>> os.system('lss')
sh: lss: command not found
32512
>>>


\\第一种,我们可以肉眼识别正确的会返回0,错误的则是非0
\\第二种,使用if判断调用系统命令返回值是否为0,如为0则不输出,不为0则输出 "Without the command"
-------------------------------错误-------------------------------------

>>> if os.system('lss')  !=0:print 'Without the command'
...

sh: lss: command not found
Without the command

--------------------------------正确------------------------------------

>>> if os.system('ls')  !=0:print 'Without the command'
...

anaconda-ks.cfg  install.log.syslog  模板  图片  下载  桌面
install.log         公共的                     视频  文档  音乐
>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息