day07_subprocess模块学习
2016-07-26 14:08
393 查看
在Python3中使用subprocess一统了系统发送命令的代码
#__author__ = 'DouYunQian'
# coding=utf-8
import subprocess
#ret=subprocess.call("appium",shell=True)#返回状态吗的命令
# print(type(ret))
#ret1=subprocess.check_call("echo hello world",shell=True)#返回状态嘛 的命令
# print(ret1)
ret2=subprocess.check_output("echo helloworld",shell=True)#返回结果的命令
# print(ret2)
#以上代码的底层是Popen
#subprocess.Popen("mkdir t3",shell=True,cwd="C:\Jv\day07\src")#执行需要到特定目录下的命令
#执行交互式的命令代码如下
fd=subprocess.Popen(['python'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)
fd.stdin.write("print(1)\n")
fd.stdin.write('print("hello world)')
fd.stdin.close()
mo2=fd.stderr.read()
print("Error: ",mo2)
mo=fd.stdout.read()
print("Out: ",mo)
out_error_list=fd.communicate()
print(out_error_list)
import subprocess
obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
obj.stdin.write("print(1)\n")
obj.stdin.write("print(2)")
out_error_list = obj.communicate()#这可以执行简单的命令,先从错误管道拿去信息,再从正确输出管道拿去信息
print(out_error_list)
#__author__ = 'DouYunQian'
# coding=utf-8
import subprocess
#ret=subprocess.call("appium",shell=True)#返回状态吗的命令
# print(type(ret))
#ret1=subprocess.check_call("echo hello world",shell=True)#返回状态嘛 的命令
# print(ret1)
ret2=subprocess.check_output("echo helloworld",shell=True)#返回结果的命令
# print(ret2)
#以上代码的底层是Popen
#subprocess.Popen("mkdir t3",shell=True,cwd="C:\Jv\day07\src")#执行需要到特定目录下的命令
#执行交互式的命令代码如下
fd=subprocess.Popen(['python'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)
fd.stdin.write("print(1)\n")
fd.stdin.write('print("hello world)')
fd.stdin.close()
mo2=fd.stderr.read()
print("Error: ",mo2)
mo=fd.stdout.read()
print("Out: ",mo)
out_error_list=fd.communicate()
print(out_error_list)
import subprocess
obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
obj.stdin.write("print(1)\n")
obj.stdin.write("print(2)")
out_error_list = obj.communicate()#这可以执行简单的命令,先从错误管道拿去信息,再从正确输出管道拿去信息
print(out_error_list)
相关文章推荐
- spring boot 使用spring AOP实现拦截器
- Spark 官方文档(3)——Standalone 模式
- ImportError: No module named apt_pkg
- Loading.LockPersistenManager 加载资源卡顿
- Android坐标getLeft,getRight,getTop,getBottom,getLocationInWindow和getLocationOnScreen
- 【优质】React的学习资源
- [转]你真的了解setTimeout和setInterval吗?
- WebLogic入门
- 判断手机系统类型以及判断是否在微信环境
- 适配器模式
- PHP获取当期前运行文件的路径,名字,服务器路径
- I,P,B帧和PTS,DTS的关系,GOP相关
- Shell脚本学习心得
- jquery中attr和prop的区别
- 传智播客:俩小时助你解决网站排名!
- 设计模式6大原则
- 使用Spring JDBCTemplate简化JDBC的操作
- 杭电-4006 The kth great number(优先队列)
- Xcode检测解决iOS内存泄露
- iOS开发 ☞ 获取本机ip地址