day07_subprocess模块学习

网友投稿 278 2022-08-23


day07_subprocess模块学习

在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)


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:手把手带你实现一个萌芽版的Spring容器
下一篇:FastAPI官方教程太棒了(上)(fastapi中文网)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~