<dd id="sga4y"><optgroup id="sga4y"></optgroup></dd>
<nav id="sga4y"><code id="sga4y"></code></nav>
  • <optgroup id="sga4y"></optgroup>
    ???? Python ???
    ??13?? ??
    ???? try..finally ????

    try..finally

    ???????????????????????????????????????????????????????????????????????finally??????¨À?????????try???¡ê???????????except????finally?î•???????????????????????????????????????

    ???finally

    ??13.3 ???finally

    #!/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'

    ????????code/finally.py??

    ???

    $ python finally.py
    Programming is fun
    When the work is done
    Cleaning up...closed the file
    Traceback (most recent call last):
      File "finally.py", line 12, in ?
        time.sleep(2)
    KeyboardInterrupt

    ????¦É???

    ?????????????????????????????????????????????time.sleep???????2???????????????????¨®??????§Ö????§»??Python??????????????§Ö¨²????????????§Ö??????Ctrl-c?§Ø?/???????

    ?????????KeyboardInterrupt???????????????????????????????????finally??????????§µ?????????


    ???? ????? ????
    ?????? ??? ????
    ?????
    @ssv 97av