python try:except:finally

后端 未结 8 2122
后悔当初
后悔当初 2020-12-23 17:36
# Open new file to write
file = None
try:
    file = open(filePath, \'w\')
except IOError:
    msg = (\"Unable to create file on disk.\")
    file.close()
    return         


        
8条回答
  •  無奈伤痛
    2020-12-23 17:56

    what is the logic in including the

    file.write("Hello World!")

    inside the finally clause?? i think it must be put in try clause itself.

    try:
            file = open(filePath, 'w')
            file.write("Hello World!")
    except IOError:
            print("Unable to create file on disk.")
    finally:
            file.close()
    

提交回复
热议问题