python¶ÁдÎļþ
1.open
ʹÓÃopen´ò¿ªÎļþºóÒ»¶¨Òª¼ÇµÃµ÷ÓÃÎļþ¶ÔÏóµÄclose()·½·¨¡£±ÈÈç¿ÉÒÔÓÃtry/finallyÓï¾äÀ´È·±£×îºóÄܹرÕÎļþ¡£
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
×¢£º²»ÄܰÑopenÓï¾ä·ÅÔÚtry¿éÀÒòΪµ±´ò¿ªÎļþ³öÏÖÒ쳣ʱ£¬Îļþ¶ÔÏófile_objectÎÞ·¨Ö´ÐÐclose()·½·¨¡£
2.¶ÁÎļþ
¶ÁÎı¾Îļþ
input = open('data', 'r')
#µÚ¶þ¸ö²ÎÊýĬÈÏΪr
input = open('data')
¶Á¶þ½øÖÆÎļþ
input = open('data', 'rb')
¶ÁÈ¡ËùÓÐÄÚÈÝ
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
¶Á¹Ì¶¨×Ö½Ú
file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )
¶ÁÿÐÐ
list_of_all_the_lines = file_object.readlines( )
Èç¹ûÎļþÊÇÎı¾Îļþ£¬»¹¿ÉÒÔÖ±½Ó±éÀúÎļþ¶ÔÏó»ñȡÿÐУº
for line in file_object:
process line
3.дÎļþ
дÎı¾Îļþ
output = open('data', 'w')
д¶þ½øÖÆÎļþ
output = open('data', 'wb')
×·¼ÓдÎļþ
output = open('data', 'w+')
дÊý¾Ý
file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )
дÈë¶àÐÐ
file_object.writelines(list_of_text_strings)
×¢Ò⣬µ÷ÓÃwritelinesдÈë¶àÐÐÔÚÐÔÄÜÉÏ»á±ÈʹÓÃwriteÒ»´ÎÐÔдÈëÒª¸ß¡£
ÔÚ´¦ÀíÈÕÖ¾ÎļþµÄʱºò£¬³£³£»áÓöµ½ÕâÑùµÄÇé¿ö£ºÈÕÖ¾Îļþ¾Þ´ó£¬²»¿
Ïà¹ØÎĵµ£º
import urllib
from HTMLParser import HTMLParser
class TitleParser(HTMLParser):
def __init__(self):
self.title = ''
self.divcontent = ''
self.readingtitle = 0
self.readingdiv = 0
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
......
#from pp3e Chapter 9.3
#############################################################################
# popup three new window, with style
# destroy() kills one window, quit() kills all windows and app; top-level
# windows have title, icon, iconify/deiconify and protocol for wm events;
# there ......
ÕÛÌÚ¼ÆËã»ú£¬±»¼ÆËã»úÕÛÌÚ£¬Ç°Ç°ºóºóÒ²ÓÐÊ®¼¸Äê¡£
×î¿É±¯µÄÊǾÓȻһÎÞËù³É£¬ËäÈ»½ö½ö³öÓÚÐËȤ£¬²¢ÎÞ¹¤×÷֮ѹÁ¦£¬µ«ÊÇÕâÖÖ´ìÕÛ¸ÐȷʵÊÇʵ´òʵµÄ¡£
ÒѾ¼Ç²»ÆðÊÇÄÄÒ»ÌìÁË£¬ºÃÏñͻȻ¾ÍÖªµÀÁËpythonµÄ´æÔÚ£¬´Ó×î³õµÄ²»¾Ò⣬µ½Ô½À´Ô½Ï²°®£¬ÎÒÒ»²½²½µÄÏòpython¿¿½ü¡£
ÎÒϲ»¶Python£¬ÕâÊÇÒ»¸öÊÂʵ£¬ÎÒ׫´ ......
ÒëÕßÑÔ£º
ÔçÔÚ 2008 Äê 8 Ô£¬ÎÒ¾ÍÔøÔÚ×Ô¼ºµÄ²©¿Í·¢±íÁËһƪ¡¶ÎªÊ²Ã´<Dive into Python>²»ÖµµÃÍÆ¼ö¡·£¨http://blog.csdn.net/lanphaday/archive/2008/08/28/2845258.aspx
£©£¬µ±Ê±ÒýÆðµÄÌÖÂ۾Ͳ»¶à˵ÁË£¬²»¹ýÊÂʵÉϵ½½ñÌìÈÔÈ»ÓÐÐí¶àÅóÓÑÁôÑÔÓëÎÒÌÖÂÛ£¬ÈÃÎÒ¼¸´ÎÔôÐIJ»ËÀ£¬ÏëдÔÙÉîÈëÅúÅС£ºÃÔ˵ÄʱºòÔÚÎÒÕæÕýÔÜ×㶯Á ......