Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Python±ê×¼¿â tracebackÄ£¿é

Example 2-18 չʾÁË traceback Ä£¿éÔÊÐíÄãÔÚ³ÌÐòÀï´òÓ¡Òì³£µÄ¸ú×Ù·µ»Ø
(Traceback)ÐÅÏ¢, ÀàËÆ䲶»ñÒ쳣ʱ½âÊÍÆ÷Ëù×öµÄ. Èç Example 2-18 Ëùʾ. 2.11.0.1. Example
2-18. ʹÓà traceback Ä£¿é´òÓ¡¸ú×Ù·µ»ØÐÅÏ¢ File: traceback-example-1.py # note!
import
Example 2-18 չʾÁË traceback Ä£¿éÔÊÐíÄãÔÚ³ÌÐòÀï´òÓ¡Òì³£µÄ¸ú×Ù·µ»Ø (Traceback)ÐÅÏ¢, ÀàËÆ䲶»ñÒ쳣ʱ½âÊÍÆ÷Ëù×öµÄ. Èç Example 2-18 Ëùʾ.
2.11.0.1. Example 2-18. ʹÓà traceback Ä£¿é´òÓ¡¸ú×Ù·µ»ØÐÅÏ¢
File: traceback-example-1.py
# note! importing the traceback module messes up the
# exception state, so you better do that here and not
# in the exception handler
# ×¢Òâ! µ¼Èë traceback »áÇåÀíµôÒ쳣״̬, ËùÒÔ
# ×îºÃ±ðÔÚÒì³£´¦Àí´úÂëÖе¼Èë¸ÃÄ£¿é
import traceback
try:
raise SyntaxError, "example"
except:
traceback.print_exc()
Traceback (innermost last):
File "traceback-example-1.py", line 7, in ?
SyntaxError: example
Example 2-19 ʹÓà StringIO Ä£¿é½«¸ú×Ù·µ»ØÐÅÏ¢·ÅÔÚ×Ö·û´®ÖÐ.
2.11.0.2. Example 2-19. ʹÓà traceback Ä£¿é½«¸ú×Ù·µ»ØÐÅÏ¢¸´ÖƵ½×Ö·û´®
File: traceback-example-2.py
import traceback
import StringIO
try:
raise IOError, "an i/o error occurred"
except:
fp = StringIO.StringIO()
traceback.print_exc(file=fp)
message = fp.getvalue()
print "failure! the error was:", repr(message)
failure! the error was: 'Traceback (innermost last):\012 File
"traceback-example-2.py", line 5, in ?\012IOError: an i/o error
occurred\012'
Äã¿ÉÒÔʹÓà extract_tb º¯Êý¸ñʽ»¯¸ú×Ù·µ»ØÐÅÏ¢, µÃµ½°üº¬´íÎóÐÅÏ¢µÄÁбí, Èç Example 2-20 Ëùʾ.
2.11.0.3. Example 2-20. ʹÓà traceback Module Ä£¿é±àÂë Traceback ¶ÔÏó
File: traceback-example-3.py
import traceback
import sys
def function():
raise IOError, "an i/o error occurred"
try:
function()
except:
info = sys.exc_info()
for file, lineno, function, text in traceback.extract_tb(info[2]):
print file, "line", lineno, "in", function
print "=>


Ïà¹ØÎĵµ£º

һƪ²»´íµÄPythonÈëÃŽ̳Ì

Ô­ÎÄ
 http://www.hetland.org/python/instant-hacking.php
Instant Hacking[Òë
ÎÄ]
ÒëÕߣº ¿Ï¶¨À´¹ý                                ......

pythonϵÄweb¿ª·¢¿ò¼Ü Django,½¨Á¢Ä£ÐÍ

Òª´´½¨ÄãµÄapp£¨½«djangoÅäÖõ½»·¾³±äÁ¿Ï£©
python manage.py startapp polls
ËüÃǽ«»á´´½¨Ò»¸öpollsÎļþ¼Ð£¬ÀïÃæµÄÄÚÈÝÊÇ£º
¡¡¡¡polls/
¡¡¡¡¡¡¡¡__init__.py
¡¡¡¡¡¡¡¡models.py
¡¡¡¡¡¡¡¡views.py
Õâ¸öĿ¼½á¹¹¾ÍÊÇappÓ¦ÓóÌÐò¡£
±àдÊý¾Ý¿âWebÓ¦ÓóÌÐòµÄµÚÒ»²½ÊǶ¨ÒåÄãµÄÄ£ÐͲ㗗»ù±¾ÉϾÍÊÇÄãµÄÊý¾Ý¿ ......

PythonÎļþµÄ¶Áд

Ïà±Èjava¶øÑÔ£¬PythonÓü¸ÐдúÂë¾Í¿ÉÒÔ´úÌæjavaÊ®À´ÐеĴúÂ룬ÕæµÄ·Ç³£²»´í
'''
Created on 2009-9-2
@author: jiangqh
'''
# file create and write
context = '''hello world
hello china '''
f = file("hello.txt",'w')
f.write(context)
f.close()
Îļþ´´½¨
#use readline() read file
f = o ......

Python Á·Ï°3 ¶¨ÒåÀ࣬Àà·½·¨£¬ÊµÀý·½·¨

Éè¼ÆÒ»¸öIPÀࣺ
Éè¼Æ1 ÒªÇ󣺳õʼ»¯Ê±¸ø¶¨ipµØÖ·²¢ÅжÏipµØÖ·µÄºÏ·¨ÐÔ
            Àà·½·¨£ºÅжÏipµØÖ·ºÏ·¨ÐÔ
            ʵÀý·½·¨£º½«ipµØַת»¯Îª10½øÖƵıíʾÐÎʽ¼°16½øÖƵıíʾÐÎʽ
   ......

pythonÄ£¿éÖ®ConfigParser: ÓÃpython½âÎöÅäÖÃÎļþ

ÔÚ³ÌÐòÖÐʹÓÃÅäÖÃÎļþÀ´Áé»îµÄÅäÖÃһЩ²ÎÊýÊÇÒ»¼þºÜ³£¼ûµÄÊÂÇ飬ÅäÖÃÎļþµÄ½âÎö²¢²»¸´ÔÓ£¬ÔÚpythonÀï¸üÊÇÈç´Ë£¬ÔÚ¹Ù·½·¢²¼µÄ¿âÖоͰüº¬ÓÐ×öÕâ¼þÊÂÇé
µÄ¿â£¬ÄǾÍÊÇConfigParser£¬ÕâÀï¼òµ¥µÄ×öһЩ½éÉÜ¡£
   
ConfigParser½âÎöµÄÅäÖÃÎļþµÄ¸ñʽ±È½ÏÏóiniµÄÅäÖÃÎļþ¸ñʽ£¬¾ÍÊÇÎļþÖÐÓɶà¸ösection¹¹³É£ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ