Return file handles opened with with open?
问题 I'm creating software where I want to accept compressed files. Since files are read/written everywhere, I created a utility function for opening files, that handles opening/closing for me, for some compressed filetypes. Example code: def return_file_handle(input_file, open_mode="r"): """ Handles compressed and uncompressed files. Accepts open modes r/w/w+ """ if input_file.endswith(".gz") with gzip.open(input_file, open_mode) as gzipped_file_handle: return gzipped_file_handle The problem is,