PythonÖеÄOSÄ£¿é
osÄ£¿éÌṩÁ˶àÊý²Ù×÷ϵͳµÄ¹¦Äܽӿں¯Êý.µ±osÄ£¿é±»µ¼Èëºó,Ëü»á×ÔÊÊÓ¦ÓÚ²»Í¬µÄ²Ù×÷ϵͳƽ̨,Èçposix»òNTϵͳƽ̨,osÄ£¿é»á¸ù¾Ý²»Í¬µÄƽ̨½øÐÐÏàÓ¦µÄ²Ù×÷.±¾½ÚÄÚÈݽ«¶ÔosÄ£¿éÌṩµÄº¯Êý½øÐÐÏêϸµÄ½â¶Á.
1.1 Îļþ²Ù×÷º¯Êý
1.1.1 open()º¯ÊýÌṩ´´½¨¡¢´ò¿ª¡¢ÐÞ¸ÄÎļþµÄ¹¦ÄÜ¡£
Example 1-1. Using the os Module to Rename and Remove Files
#Filename: os-example-1.py
import os
import string
def replace(file, search_for, replace_with):
# replace strings in a text file
back = os.path.splitext(file)[0] + ".bak"
temp = os.path.splitext(file)[0] + ".tmp"
try:
# remove old temp file, if any
os.remove(temp)
except os.error:
pass
fi = open(file)
fo = open(temp, "w")
for s in fi.readlines():
fo.write(string.replace(s, search_for, replace_with))
fi.close()
fo.close()
try:
# remove old backup file, if any
os.remove(back)
except os.error:
pass
# rename original to backup...
os.rename(file, back)
# ...and temporary to original
os.rename(temp, file)
# try it out!
file = "samples/sample.txt"
replace(file, "hello", "tjena")
replace(file, "tjena", "hello")
1.1.2 rename()ºÍremove()º¯Êý,¶ÔÎļþ½øÐÐÖØÃüÃûºÍɾ³ý²Ù×÷.Çë²ÎÕÕÀý1-1
1.2 Ŀ¼²Ù×÷
1.2.1 listdir()º¯Êý,·µ»ØÖ¸¶¨Ä¿Â¼ÏÂËùÓÐÎļþÃû,²¢±£´æÓÚÒ»ÁбíÖÐ.µ«µ±Ç°Ä¿Â¼±ê¼Ç(.)ºÍ¸¸Ä¿Â¼±ê¼Ç(..)²»ÔÚÆäÖÐ.
Example 1-2. Using the os Module to List the Files in a Directory
File: os-example-2.py
import os
for file in os.listdir("samples"):
Ïà¹ØÎĵµ£º
Python MySQLdb ²éѯ·µ»Ø×Öµä½á¹¹ smallfish
MySQLdbĬÈϲéѯ½á¹û¶¼ÊÇ·µ»Øtuple£¬Êä³öʱºò²»ÊǺܷ½±ã£¬±ØÐë°´ÕÕ0£¬1ÕâÑù¶ÁÈ¡£¬ÎÞÒâÖÐÔÚÍøÉÏÕÒµ½¼òµ¥µÄÐ޸ķ½·¨£¬¾ÍÊÇ´«µÝÒ»¸öcursors.DictCursor¾ÍÐС£
ĬÈϳÌÐò£º
import MySQLdb
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', d ......
“¹þ¹þ£¡ÖªµÀÎÒ²Ù×÷Ö®ÍõС°×µÄÀ÷º¦Á˰ɣ¡”
ÿµ±ÖÜÁùÍíÉÏ£¬Ð¡°××ÜÊÇÒª¸úËÞÉáµÄµÜÐÖÌô¼¸¾ÖħÊÞ¡£Æ¾½è×Å·çɧµÄ×ßλºÍâ«ËöµÄÒâʶ£¬Ð¡°××ÜÊÇЦµÀ×îºó¡£Õâ²»£¬Ð¡°×ÓÖÒ»´ÎÈ¡µÃÁËÒÔÒ»µÐ¶þµÄʤÀû¡£
“Ö÷ÒªÊǶàÏß²Ù×÷£¡”ÕâÊÇС°×¶Ô×Ô¼ºÊ¤ÀûµÄ×ܽ ......
http://www.okpython.com/bbs/thread-3367-1-2.html
ÒÔ¶¯ÊÖʵ¼ùΪÈÙ , ÒÔÖ»¿´²»Á·Îª³Ü;
ÒÔ´òÓ¡ÈÕ־ΪÈÙ , ÒÔµ¥²½¸ú×ÙΪ³Ü;
ÒÔ¿Õ¸ñËõ½øÎªÈÙ , ÒÔÖÆ±íËõ½øÎª³Ü;
ÒÔµ¥Ôª²âÊÔΪÈÙ , ÒÔÈ˹¤²âÊÔΪ³Ü;
ÒÔÄ£¿é¸´ÓÃΪÈÙ , ÒÔ¸´ÖÆÕ³ÌùΪ³Ü;
ÒÔ¶à̬ӦÓÃΪÈÙ , ÒÔ·ÖÖ§ÅжÏΪ³Ü;
ÒÔPythonicΪÈÙ , ÒÔÈßÓàÍÏí³Îª³Ü;
ÒÔ×ܽá·ÖÏíΪ ......
È«ÎÄÀ´×Ô£ºIT¹¤³Ì¼¼ÊõÍø http://www.systhinker.com/html/91/n-11591.html
×òÌìºÍ·ÉÌìÎèÕßÌÖÂÛ¾²Ì¬ÀàÐÍÓïÑԺͶ¯Ì¬ÀàÐÍÓïÑÔÓÅÁӱȽϵÄʱºò£¬Ëµµ½PythonûÓÐÖØÔØ»úÖÆµÄÎÊÌâ¡£
ºóÀ´ÏëÏëͦÓÐÒâ˼µÄ£¬°Ñ˼¿¼µÄ¾¹ý¼Ç¼ÏÂÀ´£¬»¶ÓÅÄש¡£
ÖØÔØ£¨overload£©ºÍ¸²¸Ç£¨override£©£¬ÔÚC++£¬Java£¬C#µÈ¾²Ì¬ÀàÐÍÓïÑÔÀàÐÍÓïÑÔÖУ¬ÕâÁ½¸ö ......