Python论坛  - 讨论区

标题:[python-chinese] try,finally块会分别有一个变量的作用域?

2007年05月17日 星期四 23:00

萧连衣 xiaolianyi在gmail.com
星期四 五月 17 23:00:54 HKT 2007

½ñÌì¿´¼òÃ÷½Ì³Ì(µÚ13Õ Òì³£)
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'

http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/ch13s04.html
µ÷²»Í¨, ÎÞ·¨¹Ø±ÕÎļþÁË.

±¨´í
> "D:\Python25\python.exe" -u "D:\my\while2.py"
Traceback (most recent call last):
  File "D:\my\while2.py", line 15, in 
    f.close()
NameError: name 'f' is not defined


Ó¦¸ÃÊÇtry,finally¿é¶¼»áÓÐÒ»¸ö±äÁ¿×÷ÓÃÓò°É.

´ó¼ÒÓÐʲôºÃµÄ·½·¨½â¾öÕâ¸öÎÊÌâÂð,.
 <http://python.cn/pipermail/python-chinese/2006-February/021918.html>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070517/1daa2b25/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月17日 星期四 23:46

黄毅 yi.codeplayer在gmail.com
星期四 五月 17 23:46:06 HKT 2007

On 5/17/07, 萧连衣 <xiaolianyi at gmail.com> wrote:
>
> 今天看简明教程(第13章 异常)
> 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'
>
> http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/ch13s04.html
> 调不通, 无法关闭文件了.
>
> 报错
> > "D:\Python25\python.exe" -u "D:\my\while2.py"
> Traceback (most recent call last):
>   File "D:\my\while2.py", line 15, in 
>     f.close()
> NameError: name 'f' is not defined
>
>
> 应该是try,finally块都会有一个变量作用域吧.
>
> 大家有什么好的方法解决这个问题吗,.
>  <http://python.cn/pipermail/python-chinese/2006-February/021918.html>
>

这个很正常,如果 file 打开失败,就直接跳到 finally 了,根本没有 f 这个名字。其他语言也是如此。
也许应该改成这个:

f = None
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:
    if f:
        f.close()
        print 'Cleaning up...closed the file'

-- 
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070517/f9df26bc/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月18日 星期五 00:10

刘鑫 march.liu在gmail.com
星期五 五月 18 00:10:41 HKT 2007

Èç¹ûfile´ò¿ªÊ§°Ü£¬closeÒ²¾ÍûÓÐÒâÒåÁË£¬ÕâÀïÓ¦¸ÃÔÚtryÖ®Íâ´ò¿ª°É¡£»òÕߣ¬·Åµ½with¹Ø¼ü×ÖÖС£
Áí£¬Ò»°ã´´½¨Îļþ¶ÔÏó¶¼ÊÇÓÃopen¶àһЩ£º£©¡£

ÔÚ07-5-17£¬»ÆÒã <yi.codeplayer在gmail.com> дµÀ£º
>
> On 5/17/07, ÏôÁ¬Ò <xiaolianyi在gmail.com> wrote:
> >
> > ½ñÌì¿´¼òÃ÷½Ì³Ì(µÚ13Õ Òì³£)
> > 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'
> >   http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/ch13s04.html
> >
> > µ÷²»Í¨, ÎÞ·¨¹Ø±ÕÎļþÁË.
> >
> > ±¨´í
> > > "D:\Python25\python.exe" -u "D:\my\while2.py"
> > Traceback (most recent call last):
> >   File "D:\my\while2.py", line 15, in 
> >     f.close()
> > NameError: name 'f' is not defined
> >
> >
> > Ó¦¸ÃÊÇtry,finally¿é¶¼»áÓÐÒ»¸ö±äÁ¿×÷ÓÃÓò°É.
> >
> > ´ó¼ÒÓÐʲôºÃµÄ·½·¨½â¾öÕâ¸öÎÊÌâÂð,.
> >  <http://python.cn/pipermail/python-chinese/2006-February/021918.html>
> >
>
> Õâ¸öºÜÕý³££¬Èç¹û file ´ò¿ªÊ§°Ü£¬¾ÍÖ±½ÓÌøµ½ finally ÁË£¬¸ù±¾Ã»ÓÐ f Õâ¸öÃû×Ö¡£ÆäËûÓïÑÔÒ²ÊÇÈç´Ë¡£
> Ò²ÐíÓ¦¸Ã¸Ä³ÉÕâ¸ö£º
>
> f = None
> 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:
>     if f:
>         f.close()
>         print 'Cleaning up...closed the file'
>
> --
> http://codeplayer.blogspot.com/
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
µÇɽÕßÈ¥µÇɽ£¬ÒòΪɽÔÚÄÇÀï
ÎÒÔÚ˼¿¼£¬ÒòΪÎÊÌâÔÚÄÇÀï

ÁõöÎ
March.Liu
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070518/4263d3c9/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月18日 星期五 00:29

liokm liokmkoil在gmail.com
星期五 五月 18 00:29:05 HKT 2007

ͬÉÏ¡«ÁíÍ⣬pythonÀïµÄº¯ÊýÌå¡¢Àࡢģ¿é¶¼ÊôÓÚ´úÂë¿é£¬×÷ÓÃÓòÊǶÔÓ¦ÓÚËüÃÇÕâ¸ö¼¶±ðµÄ¡£ËùÒÔtry...finallyºÍÕû¸öº¯ÊýÌåÊôÓÚͬһ¸ö×÷ÓÃÓò£¬tryÄÚµÄÃû×ÖºÍfinallyÄÚµÄÊÇͬһ¸öÃû×Ö£¬µ±È»Ç°ÌáÊÇÃû×ÖÔø±»°ó¶¨¹ý¡£

ÔÚ07-5-18£¬ÁõöÎ <march.liu在gmail.com> дµÀ£º
>
> Èç¹ûfile´ò¿ªÊ§°Ü£¬closeÒ²¾ÍûÓÐÒâÒåÁË£¬ÕâÀïÓ¦¸ÃÔÚtryÖ®Íâ´ò¿ª°É¡£»òÕߣ¬·Åµ½with¹Ø¼ü×ÖÖС£
> Áí£¬Ò»°ã´´½¨Îļþ¶ÔÏó¶¼ÊÇÓÃopen¶àһЩ£º£©¡£
>
> ÔÚ07-5-17£¬»ÆÒã <yi.codeplayer在gmail.com > дµÀ£º
> >
> > On 5/17/07, ÏôÁ¬Ò <xiaolianyi在gmail.com> wrote:
> > >
> > > ½ñÌì¿´¼òÃ÷½Ì³Ì(µÚ13Õ Òì³£)
> > > 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'
> > >   http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/ch13s04.html
> > >
> > > µ÷²»Í¨, ÎÞ·¨¹Ø±ÕÎļþÁË.
> > >
> > > ±¨´í
> > > > "D:\Python25\python.exe" -u "D:\my\while2.py"
> > > Traceback (most recent call last):
> > >   File "D:\my\while2.py", line 15, in 
> > >     f.close()
> > > NameError: name 'f' is not defined
> > >
> > >
> > > Ó¦¸ÃÊÇtry,finally¿é¶¼»áÓÐÒ»¸ö±äÁ¿×÷ÓÃÓò°É.
> > >
> > > ´ó¼ÒÓÐʲôºÃµÄ·½·¨½â¾öÕâ¸öÎÊÌâÂð,.
> > >  <http://python.cn/pipermail/python-chinese/2006-February/021918.html>
> > >
> > >
> >
> > Õâ¸öºÜÕý³££¬Èç¹û file ´ò¿ªÊ§°Ü£¬¾ÍÖ±½ÓÌøµ½ finally ÁË£¬¸ù±¾Ã»ÓÐ f Õâ¸öÃû×Ö¡£ÆäËûÓïÑÔÒ²ÊÇÈç´Ë¡£
> > Ò²ÐíÓ¦¸Ã¸Ä³ÉÕâ¸ö£º
> >
> > f = None
> > 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:
> >     if f:
> >         f.close()
> >         print 'Cleaning up...closed the file'
> >
> > --
> > http://codeplayer.blogspot.com/
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> µÇɽÕßÈ¥µÇɽ£¬ÒòΪɽÔÚÄÇÀï
> ÎÒÔÚ˼¿¼£¬ÒòΪÎÊÌâÔÚÄÇÀï
>
> ÁõöÎ
> March.Liu
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070518/4c514208/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月18日 星期五 12:53

萧连衣 xiaolianyi在gmail.com
星期五 五月 18 12:53:36 HKT 2007

лл*liokm*,*ÁõöÎ,»ÆÒã.* ÄãÃÇÈÃÎÒ¶Ô×÷ÓÃÓòÀí½â¸üÉîÁË^_^
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070518/7a0306f9/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月18日 星期五 14:12

zhangfan yalexfan在gmail.com
星期五 五月 18 14:12:24 HKT 2007

ÒòΪÎļþpoem.txt²»´æÔÚ»òÕß±ðµÄijÖÖÔ­Òò£¬µ¼ÖÂ
f = file ('poem.txt')
Å׳öÒì³£¡£
ËùÒÔfûÓб»ÊµÀý»¯Îªfile£¬Ò²¾Í²»´æÔÚÕâ¸ö±äÁ¿¡£
²¢²»ÊÇ×÷ÓÃÓòµÄÎÊÌâ¡£




zhangfan
2007-05-18



·¢¼þÈË£º ÏôÁ¬ÒÂ
·¢ËÍʱ¼ä£º 2007-05-17 23:02:31
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³­ËÍ£º 
Ö÷Ì⣺ [python-chinese] try,finally¿é»á·Ö±ðÓÐÒ»¸ö±äÁ¿µÄ×÷ÓÃÓò?

½ñÌì¿´¼òÃ÷½Ì³Ì(µÚ13Õ Òì³£)
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' 
  http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/ch13s04.html
µ÷²»Í¨, ÎÞ·¨¹Ø±ÕÎļþÁË.

±¨´í
> "D:\Python25\python.exe" -u "D:\my\while2.py" 
Traceback (most recent call last):
  File "D:\my\while2.py", line 15, in 
    f.close()
NameError: name 'f' is not defined 
 

Ó¦¸ÃÊÇtry,finally¿é¶¼»áÓÐÒ»¸ö±äÁ¿×÷ÓÃÓò°É.

´ó¼ÒÓÐʲôºÃµÄ·½·¨½â¾öÕâ¸öÎÊÌâÂð,.
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070518/8fe1e8b3/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号