python 利用python的subprocess模块执行外部命令,获取返回值
有时执行dos命令需要保存返回值
需要导入库subprocess
import subprocess
p = subprocess.Popen('ping www.baidu.com', shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
print out.splitlines()[24:27]
for line in out.splitlines():
print line
splitlines 是个列表
可以切片操作
完整代码:
# 利用python的subprocess模块执行外部命令, 并捕获stdout, stderr的输出:
# Python代码
import subprocess # print ’popen3:’ def external_cmd(cmd, msg_in=''):
try:
proc = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout_value, stderr_value = proc.communicate(msg_in)
return stdout_value, stderr_value
except ValueError as err:
# log("ValueError: %s" % err)
return None, None
except IOError as err:
# log("IOError: %s" % err)
return None, None if __name__ == '__main__':
stdout_val, stderr_val = external_cmd('ls -l')
print 'Standard Output: %s' % stdout_val
print 'Standard Error: %s' % stderr_val
输出:
Standard Output:
Standard Error: 'ls' 不是内部或外部命令,也不是可运行的程序
或批处理文件。 Process finished with exit code 0
部分内容来自网络
python 利用python的subprocess模块执行外部命令,获取返回值的更多相关文章
- python执行系统命令后获取返回值的几种方式集合
python执行系统命令后获取返回值的几种方式集合 今天小编就为大家分享一篇python执行系统命令后获取返回值的几种方式集合,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 第一种情 ...
- python执行系统命令后获取返回值
import os, subprocess # os.system('dir') #执行系统命令,没有获取返回值,windows下中文乱码 # result = os.popen('dir') #执行 ...
- python的subprocess模块执行shell命令
subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...
- Python中使用os模块执行远程命令
1. 使用os模块远程执行命令 服务端代码 1 import socket 2 import os 3 4 sh_server = socket.socket() #创建一个socket对象 5 sh ...
- JAVA中执行JavaScript代码并获取返回值
JAVA中执行JavaScript代码并获取返回值 场景描述 实现思路 技术要点 代码实现 测试方法 运行结果 改进空间 场景描述 今天在CSDN上偶然看到一个帖子对于一段字符串 “var p=‘xx ...
- python - 标准库:subprocess模块
subprocess的目的就是启动一个新的进程并且与之通信. subprocess模块中只定义了一个类: Popen. subprocess.Popen(args, bufsize=0, execut ...
- 利用commands模块执行shell命令
利用commands模块执行shell命令 用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态 ...
- golang os/exec 执行外部命令
exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. func LookPath(file string) (st ...
- python os.system重定向stdout到变量 ,同时获取返回值
Python执行系统命令的方法 os.system(),os.popen(),commands 最近在做那个测试框架的时候发现 Python 的另一个获得系统执行命令的返回值和输出的类. 最开始的时候 ...
随机推荐
- css3控制字体动态变换颜色
css3控制字体动态变换颜色 <!doctype html> <html> <head> <meta charset="utf-8"> ...
- 【fiddler】Fiddller的应用
一.fiddler抓取移动端接口 1.获取PC端IP 2.手机ip设置为与电脑同一局域网ip并配置代理 1)手机ip地址与pc地址连接同一局域网网络 2)代理设置为手动,主机名为PCip,端口号为88 ...
- linux驱动程序与菜单关联
- Linux ppp 数据收发流程
转:http://blog.csdn.net/yangzheng_yz/article/details/11526671 PPP (Point-to-Point)提供了一种标准的方法在点对点的连接上传 ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
# -*- coding:utf-8 -*- china=u'我爱你,中国' #china=china.encode('utf8') file_txt = open("zhongguo.tx ...
- 用VNC远程无线站
用VNC远程无线站 第一步: 远程一台我们可以vnc连接的linux电脑(IP1为实际IP地址) vim /etc/ssh/sshd_config 修改:69行为 X11Forwarding no 为 ...
- MYSQL8.0+ 使用JDBC查询中文乱码的问题
在建表时,附加一句 DROP TABLE IF EXISTS `sys_table`;CREATE TABLE `sys_table` ( ... ) ENGINE=InnoDB DEFAULT CH ...
- linux(1)
Linux/Unix操作系统 OS 系统软件 用户.应用程序 <-OS-> 硬件:CPU Memory Disk 外设管理软件测试方向: 被测系统主要的操作系统,监控系统资源.使用系统常用 ...
- 美团面经-java开发
美团(1)1 1 2 3 5 8...,求第n项写了个递归,面试官问了两个,n=-1,和极限最大值情况下怎么办.我回答,会导致栈的内存空间溢出.又问了,在栈里会是个怎样的过程.(2)打开摩拜单车页面 ...
- redis主从+哨兵 安装配置二
实验环境: 192.168.2.201 centos7 master sentinel 192.168.2.202 centos7 slave sentinel 192.168.2.203 cen ...