Let's say I want to open a text file for reading using the following syntax:
with open(fname,'r') as f:
# do something
pass
But if I detect that it ends with .gz, I would call gzip.open().
if fname.endswith('.gz'):
with gzip.open(fname,'rt') as f:
# do something
pass
else:
with open(fname,'r') as f:
# do something
pass
If "do something" part is long and not convenient to write in a function (e.g. it would create a nested function, which cannot be serialized), what is the shortest way to call with either gzip.open or open based on the return of fname.endswith('.gz')?