易截截图软件、单文件、免安装、纯绿色、仅160KB

[python]重定向输出

  调用一个控制台程序,获取它的标准输出,或把它的标准输出重定向到界面上,这里只介绍如何获取它的标准输出,因为原理都一样的。
使用python2.5的subprocess模块来实现。
import sys
import subprocess
def RunShellWithReturnCode(command, print_output=False,
universal_newlines=True):
"""Executes a command and returns the output from stdout and the return code.
Args:
command: Command to execute.
print_output: If True, the output is printed to stdout.
If False, both stdout and stderr are ignored.
universal_newlines: Use universal_newlines flag (default: True).
Returns:
Tuple (output, return code)
"""
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
shell=use_shell, universal_newlines=universal_newlines)
if print_output:
output_array = []
while True:
line = p.stdout.readline()
if not line:
break
print line.strip("\n")
output_array.append(line)
output = "".join(output_array)
else:
output = p.stdout.read()
p.wait()
errout = p.stderr.read()
#if print_output and errout:
# print >>sys.stderr, errout
p.stdout.close()
p.stderr.close()
return output, p.returncode
def RunShell(command, silent_ok=False, universal_newlines=True,
print_output=False):
data, retcode = RunShellWithReturnCode(command, print_output,
universal_newlines)
if retcode:
ErrorExit("Got error status from %s:\n%s" % (command, data))
if not silent_ok and not data:
ErrorExit("No output from %s" % command)
return data

   上面的代码来自于 开源的代码审查工具 rietveld 的代码,其中RunShellWithReturnCode函数 就是返回控制台的标准输出和返回结果


相关文档:

Amusing Python 2: range/xrange

这两个基本上都是在循环的时候用。
Python
代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://cloudhe.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://cloudhe.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=for%20i% ......

Python 处理字节


zz from: http://blog.sina.com.cn/s/blog_4b5039210100f1tu.html
原文有点小错误,改了一点点。
我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组,字典。但是C语言中有些字节型的变量,在python中该如何实现呢?这点颇为重要,特别是要在网络上进行数据传输的话。

python提供了一个struc ......

Python中Range和XRange的区别

Python中Range和XRange的区别(Difference between Range and XRange in Python)
最近机器出了点问题,所以一直没有写新的东西出来。之前在读Python的代码的时候,发觉好多人喜欢用XRange,而不是Range,我也只是记得学习的时候开始学到的是Range,后面又看到有个XRange,当时也没有深究,为什么Python里面要有两个同样的功 ......

用Python实现简单的遗传算法

用Python实现简单的遗传算法
2010-02-15 14:00
这几天一直在用Python写一个简单的遗传算法,好自己拿来实验。断断续续的折腾了几天,今天下午终于写了一个简单的出来。整个代码还是仿照《游戏编程中的人工智能技术》中的代码来写的。如果想简单的学习一下遗传算法以及简单应用《游戏编程中的人工智能技术》是不错的选择。 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号