最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput()

源码如下:

def getstatus(file):
"""Return output of "ls -ld <file>" in a string."""
import warnings
warnings.warn("commands.getstatus() is deprecated", DeprecationWarning, 2)
return getoutput('ls -ld' + mkarg(file)) # Get the output from a shell command into a string.
# The exit status is ignored; a trailing newline is stripped.
# Assume the command will work with '{ ... ; } 2>&1' around it..
#
def getoutput(cmd):
"""Return output (stdout or stderr) of executing cmd in a shell."""
return getstatusoutput(cmd)[1] # Ditto but preserving the exit status.
# Returns a pair (sts, output)
#
def getstatusoutput(cmd):
"""Return (status, output) of executing cmd in a shell."""
import os
pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
text = pipe.read()
sts = pipe.close()
if sts is None: sts = 0
if text[-1:] == '\n': text = text[:-1]
return sts, text

  通过查看以上源码,发现主要使用的是函数getstatusoutput()。

我调用这个函数的时候,每次的执行结果都是:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

怀疑是getstatusoutput()中的这句话pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')有问题。但是想想这是官网提供的模块,按说不会出现这种问题。

把pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r'),修改为pipe = os.popen('{ ' + cmd + '; }2>&1', 'r'),就是把2>&1前的空格去掉,结果执行没有'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件错误了,但是没有执行结果。

目前不知道怎么解决这个问题,可能是系统的问题吧。

别人使用相同的代码,执行结果正确。

python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。的更多相关文章

  1. Windows中安装jdk,出现javac不是内部或外部命令 也不是可运行的程序

    在经过多次实验,终于解决了在dos窗口验证jdk是否安装成功的错误提示-----javac不是内部或外部命令 也不是可运行的程序 首先要明确的是 在新建 JAVA_HOME , CLASSPATH文件 ...

  2. CMD 中运行 xx 命令提示 不是内部或外部命令,也不是可运行的程序或批处理文件的问题

    出现这个问题的原因一般有2个 这个命令依赖某个软件,而你又没有安装 这里你只需要去下载安装好对应的软件,基本上就可以解决上面的问题了. 软件安装好了,但是需要配置环境变量 第二个原因就按照下图,去设置 ...

  3. win10的cmd中显示:telnet不是内部或外部命令也不是可运行的程序或批处理?

    win10的cmd中显示:telnet不是内部或外部命令也不是可运行的程序或批处理? 摘录自:https://blog.csdn.net/haijing1995/article/details/664 ...

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

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

  5. python 安装时,为何pip install不是内部或者外部命令错误解决办法

    新安装的python 环境,第一次pip  install 却报不是内部或者外部命令错误 首先检查一下环境变量,可能时你没有设置环境变量 再说一遍,安装python环境时,记得出了python.exe ...

  6. python中的commands模块

    commands模块用于调用shell命令 有3中方法: commands.getstatus()   返回执行状态 commands.getoutput()   返回执行结果 commands.ge ...

  7. 'python'不是内部或外部命令,也不是可运行程序或批处理文件

    配置两个环境变量: 我的电脑——属性——高级系统设置——环境变量——用户变量——path(新建) 1.配置python\python.exe所在的路径       path新建:C:\Users\Py ...

  8. 搭建TensorFlow中碰到的一些问题(TensorBoard不是内部或外部指令也不是可运行的程序)~

    一.windows10环境+pip python软件包(最新版)+Pycharm软件(过段时间在弄下CUDA和GPU吧) 直接使用pip指令来安装tensorflow软件(如果很久没有更新pip软件包 ...

  9. cmd 中键入netstat,net等出现不是内部或外部命令,也不是可运行的程序或批处理文件

    这是环境变量的问题,查下环境变量中path项是否包含%SystemRoot%\system32;再查下%SystemRoot%\system32有没有netstat.exe这个文件

随机推荐

  1. winfrom LED时钟

    public sealed class Clock : PictureBox { public Clock() { SetStyle(ControlStyles.AllPaintingInWmPain ...

  2. NodeJS + Socket.io搭建聊天服务器

    第一步:安装node git clone https://github.com/joyent/node.git cd node git checkout v0.10.33-release ./conf ...

  3. Spark Streaming揭秘 Day3-运行基石(JobScheduler)大揭秘

    Spark Streaming揭秘 Day3 运行基石(JobScheduler)大揭秘 引子 作为一个非常强大框架,Spark Streaming兼具了流处理和批处理的特点.还记得第一天的谜团么,众 ...

  4. numpy简单入门

    声明:本文大量参考https://www.dataquest.io/mission/6/getting-started-with-numpy(建议阅读原文)   读取文件 有一个名为world_alc ...

  5. win8安装python环境和pip & easy_install工具

    最近在学python,2.7.6的版本 首先安装python2.7 官网下载地址https://www.python.org/downloads/ 下载相应版本即可,应该是一个msi的文件,默认安装到 ...

  6. poj 2947 Widget Factory

    Widget Factory 题意:有n件装饰品,有m组信息.(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1 ...

  7. Jar包下载

    到maven上面下载 http://mvnrepository.com/artifact/redis.clients/jedis/2.9.0 到jarfire去下载 http://cn.jarfire ...

  8. 【BZOJ2330】 [SCOI2011]糖果

    Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的 ...

  9. Truncating HTML attribute value in SharePoint DataFormWebPart

    <xsl:value-ofdisable-output-escaping="yes"select="@Body"/>

  10. MBProgressHUD ---

    1,MBProgressHUD常用属性和用法Demo - (void)testMBProgressHUD { NSLog(@"test MBProgressHUD "); /* 要 ...