博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python之shell
阅读量:7000 次
发布时间:2019-06-27

本文共 1359 字,大约阅读时间需要 4 分钟。

 

import subprocess# 返回命令执行结果# result = subprocess.call('ls -l', shell=True)# result = subprocess.call(['ls', '-l'], shell=False)# print(result)# subprocess.check_call(["ls", "-l"])# subprocess.check_call("exit 1", shell=True)# 好像没Python废弃了subprocess.check_output(["echo", "Hello World!"], shell=False)subprocess.check_output("exit 1", shell=True)# 2、执行复杂的系统相关命令# 1)切换目录再执行命令obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)# 2)有多行且复杂的命令使用三个接口# 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)")# obj.stdin.close()# # cmd_out = obj.stdout.read()  # 读接口# obj.stdout.close()# cmd_error = obj.stderr.read()  # 读错误接口# obj.stderr.close()# # print(cmd_out)# print(cmd_error)# 3)一次读输出# 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)# 4)简单写法# obj = subprocess.Popen(["python"], stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)# out_error_list = obj.communicate('print("hello")')# print(out_error_list)

 

转载于:https://www.cnblogs.com/wanghuixi/p/10787138.html

你可能感兴趣的文章
网络编程-粘包问题以及解决方案
查看>>
使用控制台启动Android设备模拟器
查看>>
subscription group permisson
查看>>
Flex实现页面跳转的功能可用性分析 .
查看>>
对HGE游戏引擎的一次封
查看>>
poj 2051 Argus
查看>>
div内快元素[div,p。。。]居中办法
查看>>
2017届高三(下)高考模拟(理科)数学试题(自己命题与写代码)
查看>>
swagger-editor
查看>>
Groovy与Java集成常见的坑(转)
查看>>
SpringMVC(转)
查看>>
__tostring用法,__call处理调用,__clone克隆对象
查看>>
PHP读取文件
查看>>
免费的区块链学习资料
查看>>
ILSVRC
查看>>
matlab超限像素平滑法_脉冲伏安法理论基础
查看>>
arduino 串口读取字符串_Arduino传感器教程 第24章NRF24L01 控制电舵机
查看>>
状态码202_HTTP状态码(HTTP Status Code)
查看>>
sharepoint 2010 网站集定期备份
查看>>
管理SCCM/MDT中的驱动分类
查看>>