PythonÈëÃŵÄ36¸öÀý×Ó Ö® 33
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 037
fileName = ''
while 1:
fileName = raw_input("Input a file name:")
if fileName == 'q':
break
try:
f = file(fileName, 'r')
print 'Opened a file.'
except:
print 'There is no file named', fileName
# end of try and except
f.close()
# end of while
output£º
Input a file name:haha
There is no file named haha
Input a file name:txt
There is no file named txt
Input a file name:1
There is no file named 1
Input a file name:037_Exception.py
Opened a file.
Input a file name:q
Ïà¹ØÎĵµ£º
Ê×ÏÈÒª¸ãÇå³þ£¬×Ö·û´®ÔÚPythonÄÚ²¿µÄ±íʾÊÇunicode±àÂ룬Òò´Ë£¬ÔÚ×ö±àÂëת»»Ê±£¬Í¨³£ÐèÒªÒÔunicode×÷ΪÖмä±àÂ룬¼´ÏȽ«ÆäËû±àÂëµÄ×Ö·û´®½âÂ루decode£©³Éunicode£¬ÔÙ´Óunicode±àÂ루encode£©³ÉÁíÒ»ÖÖ±àÂë¡£
decodeµÄ×÷ÓÃÊǽ«ÆäËû±àÂëµÄ×Ö·û´®×ª»»³Éunicode±àÂ룬Èçstr1.decode('gb2312')£¬±íʾ½«gb2312±àÂëµÄ×Ö·û´®×ª»»³ ......
Ä£¿éÕⶫÎ÷ºÃÏñûʲôºÃ½²µÄ£¬ÎÞ·ÇÊDZ£´æÒ»·ÝÎļþ£¬È»ºóÔÚÁíÒ»·ÝÎļþÖÐÓÃimport ºÍfrom ** import **(*)¾ÍÐÐÁË¡£
ÕâÒ»ÕÂÖ÷Òª½²µ½ÁËϸ½Ú£¬µ¼ÈëÄ£¿éPythonÀïÃæÊÇʲô´¦ÀíµÄ£¬import ºÍ from ** import **ÓÐʲô²»Ò»Ñù¡£»¹ÓоÍÊÇÔö¼ÓÁËreload()Õâ¸öº¯ÊýµÄʹÓÃ˵Ã÷¡£
ÒÔÇ°¿´µ½ÄÄÀï˵¾¡Á¿Ê¹ÓÃimport¶ø²»ÒªÊ¹ÓÃfrom ** import * ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 032
# ÆäʵcPickleÕâ¸öÄ£¿éÆðµ½µÄ×÷ÓÿÉÒÔÓÓÍêÃÀµØе÷ÁËÎļþÖеÄÄÚÈÝ£¨¶ÔÏ󣩺ʹúÂëÖеÄÒýÓÔÀ´ÐÎÈÝ
import cPickle as p # ÕâÌõÓï¾ä¸øcPickleÆðÁ˸öСÃûp
objectFileName = r'C:\Data.txt'
aList = [1, 2, 3]
f = file(objectFileName, 'w')
p.dump(aList, f)
f.close ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
A Byte Of Python
ÖйØÓڼ̳ÐÓÐÕâÑùµÄÎÄ×Ö£º
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as sala ......