python commands模块在python3.x被subprocess取代
subprocess
可以执行shell命令的相关模块和函数有:
os.system
os.spawn
os.popen --废弃
popen2.* --废弃
commands.* --废弃,3.x中被移除
import commands result = commands.getoutput('cmd') #只返回执行的结果, 忽略返回值.
result = commands.getstatus('cmd')#返回ls -ld file执行的结果.
result = commands.getstatusoutput('cmd')
#用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result). cmd执行的方式是{ cmd ; }2>&1, 这样返回结果里面就会包含标准输出和标准错误.
例子
>>> subprocess.getstatusoutput('pwd')
(0, '/home/ronny')
>>> subprocess.getoutput('pwd')
'/home/ronny'
>>> subprocess.getstatus('pwd')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'subprocess' has no attribute 'getstatus'
以上执行shell命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了更丰富的功能。
(1) call
执行命令,返回状态码(命令正常执行返回0,报错则返回1)
ret1=subprocess.call("ifconfig")
ret2=subprocess.call("ipconfig") #python3.5不是这样,依然会抛出异常导致无法对ret2赋值
print(ret1) #0
print(ret2) #1
ret = subprocess.call(["ls", "-l"], shell=False) #shell为False的时候命令必须分开写
ret = subprocess.call("ls -l", shell=True)
(2) check_call
执行命令,如果执行成功则返回状态码0,否则抛异常
subprocess.check_call(["ls", "-l"])
subprocess.check_call("exit 1", shell=True)
(3) check_output
执行命令,如果执行成功则返回执行结果,否则抛异常
subprocess.check_output(["echo", "Hello World!"])
subprocess.check_output("exit 1", shell=True)
(4) subprocess.Popen(...)
用于执行复杂的系统命令
| 参数 | 注释 |
|---|---|
| args | shell命令,可以是字符串或者序列类型(如:list,元组) |
| bufsize | 指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲 |
| stdin, stdout, stderr | 分别表示程序的标准输入、输出、错误句柄 |
| preexec_fn | 只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用 |
| close_sfs | 在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。 |
| shell | 同上 |
| cwd | 用于设置子进程的当前目录 |
| env | 用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。 |
| universal_newlines | 不同系统的换行符不同,True -> 同意使用 \n |
| startupinfo | 只在windows下有效,将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等 |
| createionflags | 同上 |
import subprocess
ret1 = subprocess.Popen(["mkdir","t1"])
ret2 = subprocess.Popen("mkdir t2", shell=True)
终端输入的命令分为两种:
- 输入即可得到输出,如:ifconfig
- 输入进行某环境,依赖再输入,如:python
import subprocess
obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',) #在cwd目录下执行命令
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)")
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)
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)
import subprocess
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)
python commands模块在python3.x被subprocess取代的更多相关文章
- python2 commands模块在python3.x被subprocess取代
subprocess 可以执行shell命令的相关模块和函数有: os.systemos.spawnos.popen --废弃popen2.* --废弃commands.* --废弃,3.x中被移除 ...
- python commands 模块
commands 模块 通过python调用系统命令 只适用于linux commands是提供linux系统环境下支持使用shell命令的一个模块 commands.getoutput(cmd) 只 ...
- 函数和常用模块【day06】:subprocess模块(十)
本节内容 1.概述 2.前言 3.subprocess模块 4.subprocess.Popen() 一.概述 我们在实际的工作中,需要跟操作系统的命令做交互,但我们如何用python去跟操作系统之间 ...
- python常用模块之subprocess
python常用模块之subprocess python2有个模块commands,执行命令的模块,在python3中已经废弃,使用subprocess模块来替代commands. 介绍一下:comm ...
- python重要模块之subprocess模块
python重要模块之subprocess模块 我们经常要通过python去执行系统的命令或者脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就相当于发起了一个新的进程, ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- python常用模块-调用系统命令模块(subprocess)
python常用模块-调用系统命令模块(subprocess) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. subproces基本上就是为了取代os.system和os.spaw ...
- python基础系列教程——Python3.x标准模块库目录
python基础系列教程——Python3.x标准模块库目录 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata ...
- Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目 ...
随机推荐
- MFC——CDC
CDC类定义的是设备上下文对象的类,有称设备环境对象类. Windows使用与设备无关的图形设备环境(DC:Device Context)进行显示. 说到CDC类就不能不提一下GdiObject——图 ...
- 权限控制-JS判断是否有权限进行操作跳转页面需要加target
妈蛋的,这问题花了我20分钟,我用来搜索代码花了18分钟,我写代码有个差生的习惯,就是抄,啥东西想做,就上网搜别人杂写,拿下来照抄改一下即可 啧啧,妈蛋的,整个一页,没有我能用到的信息,那些博客内容一 ...
- nginx rewrite和根据url参数location
最近项目中涉及到旧老项目迁移,需要在nginx上做些配置,所以简单学习了下,好记性不如烂笔头,先记下来. rewrite 首先查看下nginx是否支持rewrite: ./nginx -V 不支持说明 ...
- Lucene系列五:Lucene索引详解(IndexWriter详解、Document详解、索引更新)
一.IndexWriter详解 问题1:索引创建过程完成什么事? 分词.存储到反向索引中 1. 回顾Lucene架构图: 介绍我们编写的应用程序要完成数据的收集,再将数据以document的形式用lu ...
- e770. 确定按钮租中已选的单选按钮
When you ask a button group for the currently selected radio button, it returns the selected radio b ...
- C# 中委托和代理是一个概念吗??
刚刚看了一下资料,发现有些资料说的是代理,有的说是委托,但都是指同一样东西delegate,委托和事件是有关的,因为事件的本质就是多播委托,关于多播委托楼主可以找找资料就知道了. 不过个人觉得这两个概 ...
- autofac解析Mvc和Webapi的坑
我们在项目中很早就开始使用autofac,也以为知道与mvc和webapi集成的做法. var builder = new ContainerBuilder(); // Mvc Register bu ...
- js jquery 按钮点击后 60秒之后才能点击 60秒倒计时
var wait = 60; function time(o) { if (wait == 0) { $(o).attr("disabled", false); $(o).val( ...
- CentOS 7 打开关闭FirewallD防火墙端口命令
CentOS 7 使用firewalld代替了原来的iptables,使用方法如下: >>>关闭防火墙 systemctl stop firewalld.service ...
- 获取GridView中RowCommand的当前索引行(转)
获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设 ...