Python±Ê¼Ç£¨10£©
PythonÖеÄÒì³£
µ±ÄãµÄ³ÌÐòÖгöÏÖijЩÒì³£µÄ×´¿öµÄʱºò£¬Òì³£¾Í·¢ÉúÁË¡£
Ò».´¦ÀíÒì³£
ÎÒÃÇ¿ÉÒÔʹÓÃtry..exceptÓï¾äÀ´´¦ÀíÒì³£¡£ÎÒÃǰÑͨ³£µÄÓï¾ä·ÅÔÚtry-¿éÖУ¬¶ø°ÑÎÒÃǵĴíÎó´¦ÀíÓï¾ä·ÅÔÚexcept-¿éÖС£
ÀýÈç:
#!/usr/bin/python
# Filename: try_except.py
import sys
try:
s = raw_input('Enter something --> ')
except EOFError:
print '\nWhy did you do an EOF on me?'
sys.exit() # exit the program
except:
print '\nSome error/exception occurred.'
# here, we are not exiting the program
print 'Done'
Êä³öΪ:
$ python try_except.py
Enter something -->
Why did you do an EOF on me?
$ python try_except.py
Enter something --> Python is exceptional!
Done
¶þ.Òý·¢Òì³£
Äã¿ÉÒÔʹÓÃraiseÓï¾äÒý·¢Òì³£¡£Ä㻹µÃÖ¸Ã÷´íÎó/Òì³£µÄÃû³ÆºÍ°éËæÒì³£´¥·¢µÄÒì³£¶ÔÏó¡£Äã¿ÉÒÔÒý·¢µÄ´íÎó»òÒì³£Ó¦¸Ã·Ö±ðÊÇÒ»¸öError»òExceptionÀàµÄÖ±½Ó»ò¼ä½Óµ¼³öÀà¡£
ÀýÈç:
#!/usr/bin/python
# Filename: raising.py
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
s = raw_input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s), 3)
# Other work can continue as usual here
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException, x:
print 'ShortInputException: The input was of length %d, \
was expecting at least %d' % (x.length, x.atleast)
else:
print 'No exception was raised.'
Êä³öΪ:
$ python raising.py
Enter something -->
Why did you do an EOF on me?
$ python raising.py
Enter something --> ab
ShortInputException: The input was of length 2, was expecting at least 3
$ python raising.py
Enter something --> abc
No exception was raised.
Èý.try..finally
¼ÙÈçÄãÔÚ¶ÁÒ»¸öÎļþµÄʱºò£¬Ï£ÍûÔÚÎÞÂÛÒì³£·¢ÉúÓë·ñµÄÇé¿ö϶¼¹Ø±ÕÎļþ£¬¸ÃÔõô×öÄØ£¿Õâ¿ÉÒÔʹÓÃfinally¿éÀ´Íê³É¡£×¢Ò⣬ÔÚÒ»¸
Ïà¹ØÎĵµ£º
żȻÐèÒªÓõ½ÕâÑùÒ»¸öº¯Êý£¬ÔÚDelphiÖУ¬ÓÐÏֳɵĺ¯Êý¿ÉÒÔµ÷Óã¡ÔÚpythonÖУ¬ÎÒÕÒÁ˰ëÌì¶¼»¹Ã»ÕÒµ½£¬×îºó×Ô¼ºÐ´ÁËÒ»¸öº¯ÊýÈçÏ£º
def dayOfMonth(date):
if date.month == 12:
return 31
else:
return (date.replace(month=date.month+1, day=1) - datetime.timedelta(days=1)).day
......
¼Ìǰƪ¡¶Import Module¡·£¨http://blog.csdn.net/xiadasong007/archive/2009/09/02/4512797.aspx£©£¬¼ÌÐø·ÖÎöǶÈ벿·Ö»ù´¡ÖªÊ¶¡£Õâ´Î²»¶à˵£¬ÓÐʲôÎÊÌâ¼ÇµÃ¶à²éÓ¢ÎÄ×ÊÁÏ£¬¹úÄÚµÄÕâ·½ÃæÖªÊ¶ÉÙ
»¹ÊÇÀ´¿´´úÂ룬дÍêÎÒ¾Í˯¾õÁË~
#include "python/python.h"
#include <iostream>
using namespace std;
int ......
¿Í»§¸øÒ»¶ÑͼƬҪ´«µ½ºǫ́£¬Í¼Æ¬Ì«´óÁË£¬ÉϰÙÕÅͼÓÃphotoshop¸ÄÌ«Âý£¬¾ÍÏëµ½ÓÃpythonд¸ö¼òµ¥µÄÅú´¦Àí¡£¹¦Äܼòµ¥¾ÍÊǰÑÔͼ°´±ÈÀýËõС£¬´úÂë¸ü¼òµ¥ 20¶àÐС£
# -*- coding: cp936 -*-
import Image
import glob, os
#ͼƬÅú´¦Àí
def timage():
for files in glob.glob('D:\\1\\*.JPG'):
filepath,filena ......
import sys
import os
import datetime
import time
class ArgsDealwith:
def arg_environment(self, args):
filepath = ('PYTHON_PATH', 'path')
for i in filepath:
&nbs ......
PythonµÄÃæÏò¶ÔÏóÐÔÖÊ
ÀàºÍ¶ÔÏóÊÇÃæÏò¶ÔÏó±à³ÌµÄÁ½¸öÖ÷Òª·½Ãæ¡£Àà´´½¨Ò»¸öÐÂÀàÐÍ£¬¶ø¶ÔÏóÕâ¸öÀàµÄ ʵÀý ¡£ÕâÀàËÆÓÚÄãÓÐÒ»¸öintÀàÐ͵ıäÁ¿£¬Õâ´æ´¢ÕûÊýµÄ±äÁ¿ÊÇintÀàµÄʵÀý£¨¶ÔÏ󣩡£
ÐèҪעÒâµÄÊÇ£¬ÔÚPythonÖУ¬¼´±ãÊÇÕûÊýÒ²±»×÷Ϊ¶ÔÏó£¨ÊôÓÚintÀࣩ¡£ÕâºÍC++¡¢Java£¨1.5°æÖ®Ç°£©°ÑÕûÊý´¿´â×÷ΪÀàÐÍÊDz»Í¬µÄ¡£Í¨¹ ......