subprocess.Popen(...)

python3 实现代码: 备注write 要使用bytes 在最后结果转为str 显示时才不会出现 b'' 这样的字节显示

import subprocess

obj = subprocess.Popen(["python3"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write(bytes('print(1)',encoding='utf-8'))
# obj.stdin.write('print 2 \n ')
# obj.stdin.write('print 3 \n ')
# obj.stdin.write('print 4 \n ')
obj.stdin.close() cmd_out = obj.stdout.read()
obj.stdout.close()
cmd_error = obj.stderr.read()
obj.stderr.close() print(str(cmd_out,encoding='utf-8'))
print(cmd_error)


异常处理:
备注:python3 中
try:
except Exception as e :

______________________________________________________________
while True:
num1 = input('num1')
num2 = input("num2")
try:
num1 = int(num1)
num2 = int(num2)
result = num1 + num2
except Exception as e :
print("出现异常,信息如下:")
print(e)
其他异常
try:
# 主代码块
pass
except KeyError,e:
# 异常时,执行该块
pass
else:
# 主代码块执行完,执行该块
pass
finally:
# 无论异常与否,最终执行该块
pass
主动异常和断言
while True:
num1 = input('num1')
num2 = input("num2")
try:
num1 = int(num1)
raise Exception('错误了。。。') #主动异常
num2 = int(num2)
assert 1 ==1 #断言
result = num1 + num2
print(result)
except Exception as e :
print("出现异常,信息如下:")
print(e)
												

python 杂七杂八 :的更多相关文章

  1. python杂七杂八小问题

    1.win7系统下,安装完GTK+后,从命令行界面无法启动ipython,提示“failed to create process”.运行easy_install也遇到了这个问题. 原因是安装GTK+时 ...

  2. python杂七杂八知识点

    1.中文编码问题解决办法:# _*_ coding:UTF8 _*_ 2.numpy.ndArray a = array([[1,2,3], [4, 5, 6]]) 3.numpy.argsort() ...

  3. MVVM模式

    MVVM的最大缺点貌似是,报错后不好找, 在安卓6.0的时候出现了一个工具叫做databinding,其中呢主要是用来帮助实现MVVM模式的快速开发   在使用databinding的时候我们需要做的 ...

  4. Python面向对象:杂七杂八的知识点

    为什么有这篇"杂项"文章 实在是因为python中对象方面的内容太多.太乱.太杂,在写相关文章时比我所学过的几种语言都更让人"糟心",很多内容似独立内容.又似相 ...

  5. Python 小知识 杂七杂八 随手记

    1.assert 断言语句 例1:    print ‘11111111111’ assert 1==2 print ‘22222222’ 如果没有 assert 程序会输出  ‘1111111111 ...

  6. python书籍推荐以及杂七杂八

    黄哥推荐学习Python 10本好书 Python3网络爬虫开发实战 github daily-reading 想对大家说的话 POJ 算法练习 http://openjudge.cn/ http:/ ...

  7. 转战网站后台与python

    这么长时间了,迷茫的大雾也逐渐散去,正如标题所写的一样,转战网站后台开发.这段时间没怎么写博客,主要还是太忙,忙着期末考试的预习,以及服务器的搭建,python的学习,还有各种各样杂七杂八的小事,就像 ...

  8. 收藏的技术文章链接(ubuntu,python,android等)

    我的收藏 他山之石,可以攻玉 转载请注明出处:https://ahangchen.gitbooks.io/windy-afternoon/content/ 开发过程中收藏在Chrome书签栏里的技术文 ...

  9. python scrapy 抓取脚本之家文章(scrapy 入门使用简介)

    老早之前就听说过python的scrapy.这是一个分布式爬虫的框架,可以让你轻松写出高性能的分布式异步爬虫.使用框架的最大好处当然就是不同重复造轮子了,因为有很多东西框架当中都有了,直接拿过来使用就 ...

随机推荐

  1. 你搞懂 ORACLE、 SQLSERVER、MYSQL与DB2的区别了吗

    ORACLE. SQLSERVER.MYSQL与DB2的区别--平台性:    Oracle.MYSQL与DB2可在所有主流平台上运行:    SQL Server只能在Windows下运行: --安 ...

  2. CSS3鼠标滑过动画线条边框特效

    基于CSS属性animation动画制作,效果流畅弹性十足 效果展示 http://hovertree.com/texiao/css3/32/ 源码下载:http://hovertree.com/h/ ...

  3. 3.3 js函数

    1.函数语法: 函数声明的方式:function 函数名(参数1,参数2-){//函数体;}函数调用:函数名(参数1,参数2-); 函数内不一定都指定返回值. 如果需要指定返回值,可用 return ...

  4. TileJSON

    TileJSON TileJSON is an open standard for representing map metadata. License The text of this specif ...

  5. android 自定义控件——(四)圆形进度条

    ----------------------------------↓↓圆形进度条(源代码下有属性解释)↓↓---------------------------------------------- ...

  6. Linix登录报"/etc/profile: line 11: syntax error near unexpected token `$'{\r''"

    同事反馈他在一测试服务器(CentOS Linux release 7.2.1511)上修改了/etc/profile文件后,使用source命令不能生效,让我帮忙看看,结果使用SecureCRT一登 ...

  7. C#冒泡排序算法

    用了两种形式的数据,一个是泛型List,一个是数据int[].记录一下,作为自己学习过程中的笔记. using System; using System.Collections.Generic; us ...

  8. linux基本知识

    1.默认不写端口号就是80端口   127.0.0.1.localhost都代表本机 2.linux下的用户管理: id:可以查看当前用户whoami:查看当前的用户who:看当前已经登录的用户w:也 ...

  9. Sql基础

    SELECT 列名称 FROM 表名称 SELECT * FROM 表名称 SELECT DISTINCT Company FROM Orders 去重 SELECT 列名称 FROM 表名称 WHE ...

  10. .htaccess添加Header set Cache-Control报错500

    在优化网站开启站点的图片缓存时,需要在.htaccess文件中加入: #文件缓存时间配置10分钟 <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf ...