[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º¯Êý ¾ÍÊÇ·µ»Ø¿ØÖÆÌ¨µÄ±ê×¼Êä³öºÍ·µ»Ø½á¹û
Ïà¹ØÎĵµ£º
Python ÓÃÏ»®Ïß×÷Ϊ±äÁ¿Ç°×ººÍºó׺ָ¶¨ÌØÊâ±äÁ¿¡£
_xxx
²»ÄÜÓÃ'from module import *'µ¼Èë
__xxx__ ϵͳ¶¨ÒåÃû×Ö
__xxx
ÀàÖеÄ˽ÓбäÁ¿Ãû
ºËÐÄ·ç¸ñ£º±ÜÃâÓÃÏ»®Ïß×÷Ϊ±äÁ¿ÃûµÄ¿ªÊ¼¡£
ÒòΪÏ»®Ïß¶Ô½âÊÍÆ÷ÓÐÌØÊâµÄÒâÒ壬¶øÇÒÊÇÄÚ½¨±êʶ·ûËùʹÓõķûºÅ£¬ÎÒÃǽ¨Òé³ÌÐò ......
±ê׼ͷ²¿
#!/usr/bin/python
±àÂë
ËùÓÐµÄ Python ½Å±¾Îļþ¶¼Ó¦ÔÚÎļþÍ·±êÉÏ #-*- coding:utf8 -*- ¡£ÉèÖÃ±à¼Æ÷£¬Ä¬Èϱ£´æÎª utf8 ¸ñʽ¡£
mainº¯Êý
Ó¦ÓóÌÐòÒª¶¨Òåmainº¯Êý¶ø²»ÒªÖ±½Óд½Å±¾
def main():
dosomething()
if
__name__ == "__main__"
:
main()
Èç¹ûÊÇÈ«¾Ö±äÁ¿£¬ÔòÐèÒªÏÔʽ¼ÓÉ ......
python ûÓÐÊý×éµÄÊý¾Ý½á¹¹
#list ÐòÁÐ
sample_list = [1,2,3,'abc']
#dictionary ×Öµä
sample_dic = {"key":value, 2:3}
#tuple Ö»¶ÁµÄÐòÁÐ
sample_tuple = (1,3,"ab")
Ðò±í
Ðò±íÓÉһϵÁÐÖµÓöººÅ·Ö¸ô¶ø³É
Ðò±íÓë×Ö·û´®Ò»ÑùÊDz»¿É±äµÄ£º²»ÔÊÐí¶ÔÐ ......
zz from: http://blog.sina.com.cn/s/blog_4b5039210100f1tu.html
ÔÎÄÓеãС´íÎ󣬸ÄÁËÒ»µãµã¡£
ÎÒÃÇÖªµÀpythonÖ»¶¨ÒåÁË6ÖÖÊý¾ÝÀàÐÍ£¬×Ö·û´®£¬ÕûÊý£¬¸¡µãÊý£¬ÁÐ±í£¬Ôª×飬×ֵ䡣µ«ÊÇCÓïÑÔÖÐÓÐЩ×Ö½ÚÐ͵ıäÁ¿£¬ÔÚpythonÖиÃÈçºÎʵÏÖÄØ£¿ÕâµãÆÄÎªÖØÒª£¬ÌرðÊÇÒªÔÚÍøÂçÉϽøÐÐÊý¾Ý´«ÊäµÄ»°¡£
pythonÌṩÁËÒ»¸östruc ......
PythonÖÐRangeºÍXRangeµÄÇø±ð(Difference between Range and XRange in Python)
×î½ü»úÆ÷³öÁ˵ãÎÊÌ⣬ËùÒÔһֱûÓÐдÐµĶ«Î÷³öÀ´¡£Ö®Ç°ÔÚ¶ÁPythonµÄ´úÂëµÄʱºò£¬·¢¾õºÃ¶àÈËϲ»¶ÓÃXRange£¬¶ø²»ÊÇRange£¬ÎÒÒ²Ö»ÊǼǵÃѧϰµÄʱºò¿ªÊ¼Ñ§µ½µÄÊÇRange£¬ºóÃæÓÖ¿´µ½ÓиöXRange£¬µ±Ê±Ò²Ã»ÓÐÉ£¬ÎªÊ²Ã´PythonÀïÃæÒªÓÐÁ½¸öͬÑùµÄ¹¦ ......