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环境进行修改。

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

最新文章

  1. 分享10款非常有用的 Ajax 插件
  2. DevExpress使用的过期版本解决方法
  3. TYVJ博弈论
  4. Grunt之项目脚手架
  5. angularJS中controller的通信
  6. 让未激活的win8.1不再跳出提示激活的窗口
  7. python2 和python3共存下问题
  8. 将archlinux 2013-06-01版,安装配置为个人工作站
  9. 【转】python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)
  10. TCP/UDP基本概念部分
  11. linux vi 中s 替换方法
  12. ListView判断滑动底部
  13. JAVA多态问题总结(课堂总结)
  14. PHP 函数漏洞总结
  15. linux为什么不可以添加硬链接
  16. EF优化之启动预热
  17. 带缓冲I/O 和不带缓冲I/O的区别与联系
  18. springboot tomcat配置参数列表
  19. 实体类和json互相转换
  20. 【python深入】dict和list实现排序:sorted()和lambda的使用

热门文章

  1. Metaphor of topological basis and open set
  2. Round#534 div.2-C Grid game
  3. SpringCloud教程 | 第四篇:断路器(Hystrix)
  4. ns2.34移植leach协议
  5. java线程系列之三(线程协作)
  6. MVC:添加Html辅助器
  7. Vue(二十五)打包后路径报错问题
  8. Hibernate-day04
  9. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
  10. [Luogu P1495]曹冲养猪