Avoiding reading the file multiple time. Use 'pythonic' way to open and close file