Python ºÍ bash shell½Å±¾
×÷Ϊһ¸ö³õѧshellµÄ³ÌÐòÔ±À´Ëµ£¬Ê¹ÓÃshellµÄһЩÃüÁîµ÷Ó㬱ÈÈç¾Íд¼¸ÐУ¬×öһЩ×Ô¶¯»¯µÄ²Ù×÷£¬¼òµ¥ÓÖʵÓ㬵«ÊÇÉæ¼°Âß¼¿ØÖƺÍ
×Ö·û´®´¦Àíʱ£¬¿´µ½ÄÇif/fi£¬case£¯esac……»¹ÓйÅÀë¹Å¹ÖµÄ²¼¶ûÅжυ…»¹Óи´ÔÓ¶øÓÖÃû×ÖºÜÏì¶øÇÒºÜÀÏ×ʸñµÄsed¡¢awk……ÎÒ¾ÍÏ룬
ÔÚûÓзÇÒªÔÚshellÖÐдµÄÃüÁîµÄʱºò£¬¸ÉÂï²»ÓÃpythonÄØ£¿
µ±ÕæÓзÇÒªÔÚshellÖÐдµÄÃüÁîµÄʱºò£¬ shellºÍpythonºÏÆðÀ´Ò²ºÜHIѽ£¬ÏëÏë1KÐеÄshellºÍ1KÐеÄpython¶ÁÆðÀ´¡¢Ð´ÆðÀ´¡¢µ÷ÊÔÆðÀ´¡¢Î¬»¤ÆðÀ´ÓÐʲôÇø±ð¡£
QUOTE£º
Ó¦ÓÃpython±àдshell½Å±¾
½ñÌìͬʽÐÎÒ±àдһ¸öshell½Å±¾¡£»°Ëµ£¬ËäÈ»ÎÒÊÜ*nixµÄÓ°ÏìÉõÉµ«ÊǶÔÓÚ*nixÀïËæ´¦¿É¼ûµÄsh½Å±¾È´ÊÇÌÖÑáÖ®¼«¡£ÎªÊ²Ã´ÌÖÑáÄØ£¿Ê×ÏÈ ÊÇÒòΪsh½Å±¾ÄÇĪÃûÆäÃîµÄÓï·¨£¬¸Ð¾õ¾ÍÏñËæдËæÈӵijÌÐò£¬ÍêȫûÓÐÈκÎÃÀ¸Ð¿ÉÑÔ¡£Æä´ÎÊÇsh½Å±¾µÄ´¦ÀíÄÜÁ¦»¹ÊDZȽÏÈõµÄ£¬ÔÚÎı¾´¦Àí¡¢XML´¦Àí»¹ÓÐÍø Âç±à³Ì·½Ã棬»ù±¾É϶¼Òª½èÖúÓÚperl,awkµÈÒ»¶Ñ³ÌÐò¡£ÎÒ¶ÔÕâЩ³ÌÐòÒ²ÊDz»´óϲ»¶µÄ£¬¿öÇÒѧϰµÚÈý·½Èí¼þ×ÜҪʱ¼ä£¬»¹²»È綼ÓÃpythonºÃÁË¡£
ÄÇ£¬python¿ÉÒÔ×öshell½Å±¾Â𣿠Ê×ÏȽéÉÜÒ»¸öº¯Êý£º
os.system(command)
Õâ¸öº¯Êý¿ÉÒÔµ÷ÓÃshellÔËÐÐÃüÁîÐÐcommand²¢ÇÒ·µ»ØËüµÄ·µ»ØÖµ¡£ÊÔÒ»ÏÂÔÚpythonµÄ½âÊÍÆ÷ÀïÊäÈëos.system(”ls -l”)£¬¾Í¿ÉÒÔ¿´µ½”ls”ÁгöÁ˵±Ç°Ä¿Â¼ÏµÄÎļþ¡£¿ÉÒÔ˵£¬Í¨¹ýÕâ¸öº¯Êý£¬python¾ÍÓµÓÐÁËshellµÄËùÓÐÄÜÁ¦¡£ºÇºÇ¡£¡£²»¹ý£¬Í¨³£ÕâÌõÃüÁî²» ÐèÒªÓõ½¡£ÒòΪshell³£ÓõÄÄÇЩÃüÁîÔÚpythonÖÐͨ³£ÓжÔÓ¦¶øÇÒͬÑù¼ò½àµÄд·¨¡£
shellÖÐ×î³£ÓõÄÊÇlsÃüÁpython¶ÔÓ¦µÄд·¨ÊÇ£ºos.listdir(dirname)£¬Õâ¸öº¯Êý·µ»Ø×Ö·û´®ÁÐ±í£¬ÀïÃæÊÇËùÓеÄÎļþÃû£¬²»¹ý²»°üº¬”.”ºÍ”..”¡£Èç¹ûÒª±éÀúÕû¸öĿ¼µÄ»°¾Í»á±È½Ï¸´ÔÓÒ»µã¡£ÎÒÃǵÈÏÂÔÙ˵°É¡£ÏÈÔÚ½âÊÍÆ÷ÀïÊÔһϣº
>>> os.listdir(”/”)
[’tmp’, ‘misc’, ‘opt’, ‘root’, ‘.autorelabel’, ’sbin’, ’srv’, ‘.autofsck’, ‘mnt’, ‘usr’, ‘var’, ‘etc’, ’selinux’, ‘lib’, ‘net’, ‘lost+found’, ’sys’, ‘media’, ‘dev’, ‘proc’, ‘boot&rsq
Ïà¹ØÎĵµ£º
ÏÂÃæÕâ¸öС¹¤¾ß°üº¬ÁË ÅжÏunicodeÊÇ·ñÊǺº×Ö£¬Êý×Ö£¬Ó¢ÎÄ£¬»òÕßÆäËû×Ö·û¡£ È«½Ç·ûºÅת°ë½Ç·ûºÅ¡£ unicode×Ö·û´®¹éÒ»»¯µÈ¹¤×÷¡£ »¹ÓÐÒ»¸öÄÜ´¦Àí¶àÒô×ֵĺº×ÖתƴÒôµÄ³ÌÐò£¬»¹ÔÚÕûÀíÖС£
#!/usr/bin/env python
# -*- coding:GBK -*-
"""ºº×Ö´¦ÀíµÄ¹¤¾ß:
ÅжÏunicodeÊÇ·ñÊǺº×Ö£¬Êý×Ö£¬Ó¢ ......
Îļþ²Ù×÷ÊdzÌÐòÉè¼ÆÖв»¿É»òȱµÄÖØÒª²¿·Ö¡£Pythonͨ¹ýÒ»¸öÄÚÖú¯ÊýopenÀ´´ò¿ªÎļþ¡£
open(filename,mode,buffer)
ÆäÖеÚÒ»¸ö²ÎÊýÊÇÒª´ò¿ªµÄÎļþµÄÎļþÃû£¬±ØÑ¡£»µÚ¶þ¸öÊÇ´ò¿ª·½Ê½£¬¿ÉÑ¡£»µÚÈý¸öΪ»º³åÇø£¬¿ÉÑ¡¡£Ä¬ÈÏÇé¿öÏÂÊÇÒÔ“¶Á”ģʽ´ò¿ªÎļþ¡£¸Ãº¯Êý ......
mysqlÊÇÒ»¸öÓÅÐãµÄ¿ªÔ´Êý¾Ý¿â£¬ËüÏÖÔÚµÄÓ¦Ó÷dz£µÄ¹ã·º£¬Òò´ËºÜÓбØÒª¼òµ¥µÄ½éÉÜÒ»ÏÂÓÃpython²Ù×÷mysqlÊý¾Ý¿âµÄ·½·¨¡£python²Ù×÷Êý¾Ý¿âÐèÒª°²×°Ò»¸öµÚÈý·½µÄÄ£¿é£¬ÔÚhttp://mysql-python.sourceforge.net/
ÓÐÏÂÔغÍÎĵµ¡£
ÓÉÓÚpythonµÄÊý¾Ý¿âÄ£¿éÓÐרÃŵÄÊý¾Ý¿âÄ£¿éµÄ¹æ·¶£¬ËùÒÔ£¬Æäʵ²»¹ÜʹÓÃÄÄÖÖÊý¾ ......
±àÒëPython½Å±¾Éú³É¿ÉÖ´ÐгÌÐò£¬¿ÉÒÔͨ¹ýpy2exe±àÒëÍê³É¡£
ÔÚVCÖе÷ÓÃpythonʱ£¬¿ÉÒÔ½áºÏpy2exe£¬Íê³ÉÕû¸ö³ÌÐòµÄ¶ÀÁ¢·¢²¼^_^
google.py:
#! /usr/bin/env python
import sys, webbrowser
def main(a):
url = "http://www.google.com/search?q=%s" % a
webbrowser.o ......