readlines reads the entire file. So this is not for large files.
Executing 2 context blocks as you do, and then using the input_file descriptor outside of them does not work (operation in a closed file).
To get the best of both worlds, I would use a ternary conditional for the context block (which determines whether to use open or gzip.open ), and then iterate through the lines.
open_function = gzip.open if ".gz" in FILE_LIST['INPUT_FILE'] else open with open_function(FILE_LIST['INPUT_FILE'],"r") as input_file: for line in input_file:
Notice that I added the "r" mode to work with non-binary text ( gzip.open defaults to binary)
Alternative: open_function can be made common, so it does not depend on FILE_LIST['INPUT_FILE'] :
open_function = lambda f: gzip.open(f,"r") if ".gz" in f else open(f)
after determining, you can reuse it as desired
with open_function(FILE_LIST['INPUT_FILE']) as input_file: for line in input_file:
source share