Python论坛  - 讨论区

标题:[python-chinese] try-finally的使用错误

2006年11月22日 星期三 20:29

shirj shirj在livedoor.cn
星期三 十一月 22 20:29:09 HKT 2006

pythoners,

是不是我眼花了,下面的代码有编译错误,5555555

.................................................
        outfilew = None
        try:
            outfilew = open(outfile,'a',1024)
            outfilew.write('Find %s lines in file: %s' % (
len(outLines), filename))
            outfilew.writelines(outLines)
        except IOError:
            print 'Find %s lines in file: %s' % ( len(outLines), filename)
            print '\n'.join(outLines)
        finally :
            if outfilew:outfilew.close()

.................................................

> D:\software\Python24\pythonw.exe -u "D:\_tmp_\listjava.py"
  File "D:\_tmp_\listjava.py", line 47
    finally :
          ^
SyntaxError: invalid syntax

这是什么错误???

-- 
    _
  >` )     - msn: trydofor在hotmail.com
  ( ( \    - url: www.trydofor.com
---``|\ ----------------------------------------
            ___|___   __|___|__     |____
           |   |   |  __|___|__    /    /
           |___|___| |         | _/    /
             __/       ---|---        /\
              /\         /|\         /  \
           __/  \____ __/ | \__  ___/    \_


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

2006年11月22日 星期三 20:34

Zoom.Quiet zoom.quiet在gmail.com
星期三 十一月 22 20:34:53 HKT 2006

On 11/22/06, shirj <shirj在livedoor.cn> wrote:
> pythoners,
>
> 是不是我眼花了,下面的代码有编译错误,5555555
>
try_stmt  	::=  	try_exc_stmt | try_fin_stmt
try_exc_stmt 	::= 	"try" ":" suite
		("except" [expression ["," target]] ":" suite)+
		["else" ":" suite]
try_fin_stmt 	::= 	"try" ":" suite "finally" ":" suite

Try 的语法是  except 和 不能混合使用!

try:
  ...
  try:
    ...
  except:
    ....
finally:
  ....

这样才可以混合的,
> .................................................
>         outfilew = None
>         try:
>             outfilew = open(outfile,'a',1024)
>             outfilew.write('Find %s lines in file: %s' % (
> len(outLines), filename))
>             outfilew.writelines(outLines)
>         except IOError:
>             print 'Find %s lines in file: %s' % ( len(outLines), filename)
>             print '\n'.join(outLines)
>         finally :
>             if outfilew:outfilew.close()
>
> .................................................
>
> > D:\software\Python24\pythonw.exe -u "D:\_tmp_\listjava.py"
>   File "D:\_tmp_\listjava.py", line 47
>     finally :
>           ^
> SyntaxError: invalid syntax
>
> 这是什么错误???
>
> --
>     _
>   >` )     - msn: trydofor在hotmail.com
>   ( ( \    - url: www.trydofor.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


-- 
"""Time is unimportant, only life important!
blog@  http://blog.zoomquiet.org/pyblosxom/
wiki@    http://wiki.woodpecker.org.cn/moin/ZoomQuiet
douban@ http://www.douban.com/people/zoomq/
____________________________________
Please use OpenOffice.org to stand for M$ office.
Please use 7-zip to stand for WinRAR.
You can get realy freedom from software.
"""

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

2006年11月22日 星期三 20:43

shirj shirj在livedoor.cn
星期三 十一月 22 20:43:01 HKT 2006

thanks,

是这样啊:)

我看到http://www.python.org/dev/peps/pep-0341/
         try:
             
         except Exception:
             
         finally:
             

是不是能在新版中支持,期望中....

Zoom.Quiet wrote:
> On 11/22/06, shirj <shirj在livedoor.cn> wrote:
>> pythoners,
>>
>> 是不是我眼花了,下面的代码有编译错误,5555555
>>
> try_stmt  	::=  	try_exc_stmt | try_fin_stmt
> try_exc_stmt 	::= 	"try" ":" suite
> 		("except" [expression ["," target]] ":" suite)+
> 		["else" ":" suite]
> try_fin_stmt 	::= 	"try" ":" suite "finally" ":" suite
> 
> Try 的语法是  except 和 不能混合使用!
> 
> try:
>   ...
>   try:
>     ...
>   except:
>     ....
> finally:
>   ....
> 
> 这样才可以混合的,
>> .................................................
>>         outfilew = None
>>         try:
>>             outfilew = open(outfile,'a',1024)
>>             outfilew.write('Find %s lines in file: %s' % (
>> len(outLines), filename))
>>             outfilew.writelines(outLines)
>>         except IOError:
>>             print 'Find %s lines in file: %s' % ( len(outLines), filename)
>>             print '\n'.join(outLines)
>>         finally :
>>             if outfilew:outfilew.close()
>>
>> .................................................
>>
>>> D:\software\Python24\pythonw.exe -u "D:\_tmp_\listjava.py"
>>   File "D:\_tmp_\listjava.py", line 47
>>     finally :
>>           ^
>> SyntaxError: invalid syntax
>>
>> 这是什么错误???
>>
>> --
>>     _
>>   >` )     - msn: trydofor在hotmail.com
>>   ( ( \    - url: www.trydofor.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
> 
> 


-- 
     _
   >` )     - msn: trydofor在hotmail.com
   ( ( \    - url: www.trydofor.com
---``|\ ----------------------------------------
             ___|___   __|___|__     |____
            |   |   |  __|___|__    /    /
            |___|___| |         | _/    /
              __/       ---|---        /\
               /\         /|\         /  \
            __/  \____ __/ | \__  ___/    \_


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

2006年11月22日 星期三 20:46

yi huang yi.codeplayer在gmail.com
星期三 十一月 22 20:46:11 HKT 2006

>
> 是这样啊:)
>
> 我看到http://www.python.org/dev/peps/pep-0341/
>         try:
>             
>         except Exception:
>             
>         finally:
>             
>
> 是不是能在新版中支持,期望中....


python2.5 里 except else finally 可以同时用。



-- 
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061122/6033f5f9/attachment.html 

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

2006年11月22日 星期三 21:13

麦田守望者 qcxhome在gmail.com
星期三 十一月 22 21:13:26 HKT 2006

2.5中可以使用try-except-finally

On 11/22/06, shirj <shirj at livedoor.cn> wrote:
> thanks,
>
> 是这样啊:)
>
> 我看到http://www.python.org/dev/peps/pep-0341/
>          try:
>              
>          except Exception:
>              
>          finally:
>              
>
> 是不是能在新版中支持,期望中....
>
> Zoom.Quiet wrote:
> > On 11/22/06, shirj <shirj at livedoor.cn> wrote:
> >> pythoners,
> >>
> >> 是不是我眼花了,下面的代码有编译错误,5555555
> >>
> > try_stmt      ::=     try_exc_stmt | try_fin_stmt
> > try_exc_stmt  ::=     "try" ":" suite
> >               ("except" [expression ["," target]] ":" suite)+
> >               ["else" ":" suite]
> > try_fin_stmt  ::=     "try" ":" suite "finally" ":" suite
> >
> > Try 的语法是  except 和 不能混合使用!
> >
> > try:
> >   ...
> >   try:
> >     ...
> >   except:
> >     ....
> > finally:
> >   ....
> >
> > 这样才可以混合的,
> >> .................................................
> >>         outfilew = None
> >>         try:
> >>             outfilew = open(outfile,'a',1024)
> >>             outfilew.write('Find %s lines in file: %s' % (
> >> len(outLines), filename))
> >>             outfilew.writelines(outLines)
> >>         except IOError:
> >>             print 'Find %s lines in file: %s' % ( len(outLines), filename)
> >>             print '\n'.join(outLines)
> >>         finally :
> >>             if outfilew:outfilew.close()
> >>
> >> .................................................
> >>
> >>> D:\software\Python24\pythonw.exe -u "D:\_tmp_\listjava.py"
> >>   File "D:\_tmp_\listjava.py", line 47
> >>     finally :
> >>           ^
> >> SyntaxError: invalid syntax
> >>
> >> 这是什么错误???
> >>
> >> --
> >>     _
> >>   >` )     - msn: trydofor at hotmail.com
> >>   ( ( \    - url: www.trydofor.com
> >> ---``|\ ----------------------------------------
> >>             ___|___   __|___|__     |____
> >>            |   |   |  __|___|__    /    /
> >>            |___|___| |         | _/    /
> >>              __/       ---|---        /\
> >>               /\         /|\         /  \
> >>            __/  \____ __/ | \__  ___/    \_
> >>
> >> _______________________________________________
> >> python-chinese
> >> Post: send python-chinese at lists.python.cn
> >> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> >> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> >> Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
>      _
>    >` )     - msn: trydofor at hotmail.com
>    ( ( \    - url: www.trydofor.com
> ---``|\ ----------------------------------------
>              ___|___   __|___|__     |____
>             |   |   |  __|___|__    /    /
>             |___|___| |         | _/    /
>               __/       ---|---        /\
>                /\         /|\         /  \
>             __/  \____ __/ | \__  ___/    \_
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese


-- 
GoogleTalk: qcxhome at gmail.com
MSN: qcxhome at hotmail.com
My Space: tkdchen.spaces.live.com
BOINC: boinc.berkeley.edu
中国分布式计算总站: www.equn.com

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号