Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5703151
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: Python/Ruby

2006-05-29 16:22:06

python的异常处理
因为有C和C++的基础,看起python没有多达的困难,今天在看书的时候,发现有一个程序解释的时候没有通过。
#!/usr/bin/python
# Filename: finally.py

import time

try:
f = file('poem.txt')
while True: # our usual file-reading idiom
line = f.readline()
if len(line) == 0:
break
time.sleep(2)
print line,
finally:
f.close()
print 'Cleaning up...closed the file'

可是解释的时候,总是出错:
wangyao@wangyao:~/bin$ python finally.py
Traceback (most recent call last):
File "finally.py", line 15, in ?
f.close()
NameError: name 'f' is not defined

加了global后还是解释出错。

最后,把f的声明放到try的外面就可以了。

晕!
阅读(2157) | 评论(0) | 转发(0) |
0

上一篇:调整系统的日期

下一篇:晕,还得改IP

给主人留下些什么吧!~~