Catching an exception while using a Python 'with' statement - Part 2

前端 未结 1 1482
独厮守ぢ
独厮守ぢ 2021-02-05 16:52

this is a continuation of question Catching an exception while using a Python 'with' statement.
I\'m quite e newbie and I tested the following code with Python 3.2 o

1条回答
  •  长发绾君心
    2021-02-05 17:50

    try:
        f = open('foo.txt', 'a')
    except IOError:
        print('error')
    else:
        with f:
            #
            # some_code
            #
    

    0 讨论(0)
提交回复
热议问题