python 上下文管理器一班

python context manager one liner

是否可以在 python 中编写单行 with 上下文管理器?

类似于:

with open('xml_file.xml') as file: file.write('<fave_food>filled dhal puri roti</fave_food>')

是的,但如果要在其中写入,则必须打开文件进行写入。