Search code examples
pythonread-eval-print-loopcontextmanager

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>')

Solution

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