使用subprocess通过shell调用另一个模块组件时,需要对返回的code进行判断。判断结果为执行失败时需要raise Exception,不然调用树过于复杂时,我们很难跟踪到异常发生的位置。sys.exit(1)虽然也可以达到对执行结果进行判断的目的,但是它难于追踪异常发生的位置。

 a.py
`-- b.py
`-- ls

a.py

 import sys, subprocess

 def exec_cmd(cmd):
"""Run shell command"""
p = subprocess.Popen(cmd,stdin = subprocess.PIPE, \
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
shell = True) log_content = p.communicate()[0] return p.returncode, log_content def main():
cmd = "python b.py"
cmd_code, cmd_log = exec_cmd(cmd)
if not cmd_code == 0:
raise Exception(cmd_log) if __name__ == '__main__':
main()

b.py

 import sys, subprocess

 def exec_cmd(cmd):
"""Run shell command"""
p = subprocess.Popen(cmd,stdin = subprocess.PIPE, \
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
shell = True) log_content = p.communicate()[0] return p.returncode, log_content def main():
cmd = """ls c.py"""
cmd_code, cmd_log = exec_cmd(cmd)
if not cmd_code == 0:
raise Exception(cmd_log) if __name__ == '__main__':
main()

运行结果:

 waterforestdeiMac:pythonCrawler waterforest$ python a.py
Traceback (most recent call last):
File "a.py", line 21, in <module>
main()
File "a.py", line 18, in main
raise Exception(cmd_log)
Exception: Traceback (most recent call last):
File "b.py", line 21, in <module>
main()
File "b.py", line 18, in main
raise Exception(cmd_log)
Exception: ls: c.py: No such file or directory waterforestdeiMac:pythonCrawler waterforest$

Python subprocess shell 编程规范的更多相关文章

  1. 01 shell编程规范与变量

    前言: 大家对shell脚本应该都不算陌生了,如果突然问你什么是shell脚本?是干什么用的?由什么组成以及怎么使用?变量的概念是什么?作用范围是什么?变量间的算术运算怎么表示?你能很容易答出来吗 本 ...

  2. Python 浅谈编程规范和软件开发目录规范的重要性

    最近参加了一个比赛,然后看到队友编程的代码,我觉得真的是觉得注释和命名规范的重要性了,因为几乎每个字符都要咨询他,用老师的话来说,这就是命名不规范的后续反应.所以此时的我意识到写一篇关于注释程序的重要 ...

  3. SHELL编程规范与变量

    shell相比标准开发语言比如JAVA,C,C++来说还是比较简单的,其实就是一堆命令的集合而已,初学者从简单的shell脚本学起,至于shell编程还是要有编程思想在里面,因此,循序渐进很重要,先研 ...

  4. python浅谈编程规范和软件开发目录规范的重要性

    前言 我们这些初学者,目前要做的就是遵守代码规范,这是最基本的,而且每个团队的规范可能还不一样,以后工作了,尽可能和团队保持一致,目前初学者就按照官方的要求即可 新人进入一个企业,不会接触到核心的架构 ...

  5. shell编程规范:引用

    Shell代码规范 作 者: 毕小朋 用 途: 规范Shell代码书写,方便查看与修改 博 客: http://blog.csdn.net/wirelessqa 参 考: http://www.ohl ...

  6. Shell编程规范:Don't use ls | grep

    目录 一.背景 二.ShellCheck的规范 三.修改写法 1.ls | grep file 2.ls | grep regexp 3.ls | grep -v multi 四.最后 五.参考 一. ...

  7. shell编程规范

    1 脚本名以.sh结尾,名称尽量见名之意,比如ClearLog.sh Clear_Log.sh clearlog.sh SerRestart.sh Ser_Restart.sh;2 尽量使用UTF-8 ...

  8. Shell脚本之编程规范和变量

    Shell脚本编程规划和变量 1.Shell脚本概述 2.Shell编程规划 3.重定向与管道 4.Shell脚本变量 1.Shell脚本概述 Shell的作用:充当"翻译官"的角 ...

  9. Shell编程-01-Shell脚本初步入门

    目录 什么是Shell 什么是Shell脚本 Shell脚本语言的种类 常用操作系统默认Shell Shell 脚本的建立和执行 脚本规范 什么是Shell     简单来说Shell其实就是一个命令 ...

随机推荐

  1. CAS (3) —— Mac下配置CAS客户端经代理访问Tomcat CAS

    CAS (3) -- Mac下配置CAS客户端经代理访问Tomcat CAS tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0_65 nginx版本: nginx-1.9 ...

  2. C#递归累计到父行

    搞了半天 写了一个算法,希望能帮到需要的朋友 效果如下 水电费用是由 就是部门水费和电费累加的,而部门水费由科室水费累加起来的 表结构 DataTable dt = new DataTable(); ...

  3. node-webkit播放目录下所有网页文件

    1.编写index.html文件 这里我们需要播放某一个目录下所有的网页文件,要做到切换不同的网页,可以在index.html中使用iframe.通过js动态改变iframe的src属性,从而动态地切 ...

  4. [从jQuery看JavaScript]-注释(comments)

    jQuery片段: /*! * jQuery JavaScript Library v1.3.2 * http://jquery.com/ * * Copyright (c) 2009 John Re ...

  5. 解决App can’t be opened because it is from an unidentified developer

    关闭设置 打开终端 输入sudo spctl --master-disable

  6. ubuntu开启SSH服务远程登录

    http://blog.csdn.net/jackghq/article/details/54974141 ubuntu开启SSH服务远程登录

  7. 解决ubuntu下mysql不能远程连接数据库的问题【转】

    Ubuntu10.04上自带的MySQL,执行了root@ubuntu:~#sudo apt-get install mysql安装完mysql-server 启动mysqlroot@ubuntu:~ ...

  8. Python C :ctypes库

    >>> import ctypes >>> from ctypes import * >>> dir(ctypes) ['ARRAY', 'Arg ...

  9. 转载:15个最受欢迎的Python开源框架

    出自:http://python.jobbole.com/72306/?replytocom=57112 15个最受欢迎的Python开源框架 Django: Python Web应用开发框架 Dja ...

  10. CentOS6.8设置开机直接进入命令行模式

    在linux图形界面中,按 “Alt+Ctrl+F1”,进入命令行界面. 切换到root用户 # vi /etc/inittab 找到下面的语句: # Default runlevel. The ru ...