Use short-circuit for less nesting
Use context manager for opening and closing the file