有时执行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模块执行外部命令,获取返回值的更多相关文章

  1. python执行系统命令后获取返回值的几种方式集合

    python执行系统命令后获取返回值的几种方式集合 今天小编就为大家分享一篇python执行系统命令后获取返回值的几种方式集合,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 第一种情 ...

  2. python执行系统命令后获取返回值

    import os, subprocess # os.system('dir') #执行系统命令,没有获取返回值,windows下中文乱码 # result = os.popen('dir') #执行 ...

  3. python的subprocess模块执行shell命令

    subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...

  4. Python中使用os模块执行远程命令

    1. 使用os模块远程执行命令 服务端代码 1 import socket 2 import os 3 4 sh_server = socket.socket() #创建一个socket对象 5 sh ...

  5. JAVA中执行JavaScript代码并获取返回值

    JAVA中执行JavaScript代码并获取返回值 场景描述 实现思路 技术要点 代码实现 测试方法 运行结果 改进空间 场景描述 今天在CSDN上偶然看到一个帖子对于一段字符串 “var p=‘xx ...

  6. python - 标准库:subprocess模块

    subprocess的目的就是启动一个新的进程并且与之通信. subprocess模块中只定义了一个类: Popen. subprocess.Popen(args, bufsize=0, execut ...

  7. 利用commands模块执行shell命令

    利用commands模块执行shell命令 用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态 ...

  8. golang os/exec 执行外部命令

    exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. func LookPath(file string) (st ...

  9. python os.system重定向stdout到变量 ,同时获取返回值

    Python执行系统命令的方法 os.system(),os.popen(),commands 最近在做那个测试框架的时候发现 Python 的另一个获得系统执行命令的返回值和输出的类. 最开始的时候 ...

随机推荐

  1. Jerry Wang在SAP社区上获得的徽章

    要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  2. python中的网络通信,socket、select、selectors、socketserver

    楔子 网络通信用于获取一个算法在本地运行所需的数据,还可以共享信息实现分布式处理,另外可以用来管理云服务. python的标准库提供了一些模块来创建网络服务以及访问现有服务ipaddress模块提供了 ...

  3. Java中Static关键字详解以及静态变量和成员变量的区别

    一.static关键字的特点 (1)修饰成员变量.成员方法(2)随着类的加载而加载(3)优先于对象存在(4)被所有对象共享(5)可以通过类名调用 它本身也可以通过对象名调用 例如:main()方法由j ...

  4. python错误大全

    1.NameError:name 'Ture' is not defined 这个是名字没有定义,也可能写错了 while True: 2.IndentationError: unindent doe ...

  5. Istio技术与实践04:最佳实践之教你写一个完整的Mixer Adapter

    Istio内置的部分适配器以及相应功能举例如下: circonus:微服务监控分析平台. cloudwatch:针对AWS云资源监控的工具. fluentd:开源的日志采集工具. prometheus ...

  6. 蓝桥杯BASIC-13 数列排序

    问题描述 给定一个长度为n的数列,将这个数列按从小到大的顺序排列.1<=n<=200 输入格式 第一行为一个整数n. 第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000. 输 ...

  7. E - GCD HDU - 2588

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...

  8. [转载]springboot--常用注解--@configration、@Bean

    springboot--常用注解--@configration.@Bean @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME ...

  9. 多种view视图和序列化--django2

    models.py from django.db import models # Create your models here. class Idc(models.Model): name = mo ...

  10. C# Tcp和Socket 网络(五)

    TcpReceive public Form1() { InitializeComponent(); new Thread(() => { IPAddress ip = IPAddress.Pa ...