Is it possible to have an optional with/as statement in python?

后端 未结 5 1112
别跟我提以往
别跟我提以往 2021-01-04 03:25

Instead of this:

FILE = open(f)
do_something(FILE)
FILE.close()

it\'s better to use this:

with open(f) as FILE:
    do_some         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 03:59

    something like:

    if file:      #it checks for None,false values no need of "if file is None"
        with open(file) as FILE:
            do_something(FILE)
    else:
        FILE=None
    

提交回复
热议问题