Python "with" statement: open file, use it, finally close 

Joined:
04/09/2007
Posts:
784

April 16, 2014 15:40:09    Last update: April 16, 2014 15:40:09
The "with" statement makes it easy to open a file, use it then finally close it, without explicitly coding with a "try...finally" block.

Example:
>>> import yaml
>>> with open("app.yaml") as f:
...     c = yaml.load(f.read())
... 
>>> c


More info: Understanding Python's "with" statement
Share |
| Comment  | Tags