category
category

Reputation: 2165

python context manager one liner

Is it possible to write a one-liner with context manager in python?

Something like:

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

Upvotes: 1

Views: 830

Answers (1)

Yann Vernier
Yann Vernier

Reputation: 15887

Yes, but you'll have to open the file for writing if you want to write in it.

Upvotes: 2

Related Questions