博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python commands包不支持windows环境与如何在windows下使用的简易方法
阅读量:4340 次
发布时间:2019-06-07

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

commands模块不支持windows环境,让我们来看看。

>>> import commands>>> print commands.getoutput('dir')'{' 不是内部或外部命令,也不是可运行的程序或批处理文件。>>>

 查看commands.getoutput的源代码:

def getoutput(cmd):    """Return output (stdout or stderr) of executing cmd in a shell."""    return getstatusoutput(cmd)[1]

 这个函数调用的是commands.getstatusoutput()函数,那查看下commands.getstatusoutput的源代码

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

从commands.getstatusoutput的代码可以看出,命令运行的时候改变成了'{ ' + cmd + '; } 2>&1',这是在linux下运行的命令,windows不支持。

所以,commands模块不支持windows环境。

那如何让它支持windows环境呢?

我的建议:

1、新建一个模块,copy commands的内容到其中

2、将getstatusoutput函数下的 '{ ' + cmd + '; } 2>&1' 改为cmd

当然getstatushan函数仍然存在问题,如果要使用的话,请参考windows环境进行修改。

 

总结,以上是我的一点心得,如果存在问题,请联系我。

 

转载于:https://www.cnblogs.com/tangdouguard/p/4290589.html

你可能感兴趣的文章
[MySQL] InnoDB三大特性之 - 插入缓冲
查看>>
Sphinx安装配置应用
查看>>
dns 域名解析
查看>>
nohup top & 问题: top: failed tty get
查看>>
详解ORACLE数据库的分区表
查看>>
Windows7下安装SQLServer2005过程详解
查看>>
reids基于订阅的聊天室
查看>>
[转载]php中的ssh2
查看>>
Centos发布java的war包后,无法访问发布的工程
查看>>
通过jquery.cookie.js实现记住用户名、密码登录功能
查看>>
HDU 4320 Arcane Numbers 1(质因子)
查看>>
[转帖]状态机的编码
查看>>
Android闪闪发光字体效果
查看>>
freeMarker(八)——程序开发指南之配置(Configuration)
查看>>
前端的书的推荐(二)
查看>>
spring 注解
查看>>
Django 学习笔记(三) --- HTML 模版加载 css、js、img 静态文件
查看>>
STL 优先队列
查看>>
[原]JavaScript 盒模型 尺寸
查看>>
Code Fisrt 更新注释到mysql数据库
查看>>