Use python "with" statement

X-Link: http://stackoverflow.com/questions/7125727/python-with-statements