在Python3中使用subprocess一统了系统发送命令的代码 #__author__ = 'DouYunQian' # coding = utf - 8 import subprocess #ret = subprocess . call ( "appium" , shell = True ) #返回状态吗的命令 # print ( type ( ret )) #ret1 =
在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)