python with open as code example
Example 1: with open as file python
>>> with open('workfile') as f:
... read_data = f.read()
>>> # We can check that the file has been automatically closed.
>>> f.closed
True
Example 2: with open
with open('output.txt', 'w') as file: # Use file to refer to the file object
file.write('Hi there!')