36. Reading a file

The key here is to use a context manager to manage resources.

36.1. Don’t do this

1f = open('README.md')
2try:
3    data = f.read()
4    print(len(data))
5finally:
6    f.close()

36.2. Do this

1with open('README.md') as f:
2    data = f.read()
3    print(len(data))