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¿éÀ´Íê³É¡£×¢Ò⣬ÔÚÒ»¸
Ïà¹ØÎĵµ£º
¼Ìǰƪ¡¶Import Module¡·£¨http://blog.csdn.net/xiadasong007/archive/2009/09/02/4512797.aspx£©£¬¼ÌÐø·ÖÎöǶÈ벿·Ö»ù´¡ÖªÊ¶¡£Õâ´Î²»¶à˵£¬ÓÐʲôÎÊÌâ¼ÇµÃ¶à²éÓ¢ÎÄ×ÊÁÏ£¬¹úÄÚµÄÕâ·½Ãæ֪ʶÉÙ
»¹ÊÇÀ´¿´´úÂ룬дÍêÎÒ¾Í˯¾õÁË~
#include "python/python.h"
#include <iostream>
using namespace std;
int ......
Ä£¿é
Ò».¼ò½é
Ä£¿é»ù±¾ÉϾÍÊÇÒ»¸ö°üº¬ÁËËùÓÐÄ㶨ÒåµÄº¯ÊýºÍ±äÁ¿µÄÎļþ¡£ÎªÁËÔÚÆäËû³ÌÐòÖÐÖØÓÃÄ£¿é£¬Ä£¿éµÄÎļþÃû±ØÐëÒÔ.pyΪÀ©Õ¹Ãû¡£
ÀýÈ磺
#!/usr/bin/python
# Filename: using_sys.py
import sys
print 'The command line arguments are:'
for i in sys.argv:
print i
print '\n ......
#coding=utf-8
from newtest.wiki.models import WiKi
from django.template import loader, Context
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
def index(request, pagename=""):
"""ÏÔʾÕý³£Ò³Ã棬¶ÔÒ³ÃæµÄÎÄ×Ö×öÌØÊâµÄÁ´½Ó´¦Àí"""
......
Ò»¸öPython½Å±¾µÄ¿ª·¢È«¹ý³Ì
ÎÊÌ⣺Íê³ÉÒ»¸ö¿ÉÒÔΪÎÒÃÇËùÓеÄÖØÒª³ÌÐò×ö±¸·ÝµÄ³ÌÐò¡£
²½Öè²ð½â£º
ÐèÒª±¸·ÝµÄÎļþºÍĿ¼ÓÉÒ»¸öÁбíÖ¸¶¨¡£
Îļþ±¸·Ý³ÉÒ»¸özipÎļþ¡£
zip´æµµµÄÃû³ÆÊǵ±Ç°µÄÈÕÆÚºÍʱ¼ä¡£
ÎÒÃÇʹÓñê×¼µÄzipÃüÁËüͨ³£Ä¬ÈϵØËæLinux/Unix·¢ÐаæÌṩ¡£WindowsÓû§¿ÉÒÔʹÓÃInfo-Zip³ÌÐò¡£×¢Òâ ......