Consider using with statement for these.  It does have more indentation, but is less error prone