您的位置:首页 > 其它

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: