def open_file(loc):
loc = ensure_path(loc)<EOL>if tarfile.is_tarfile(str(loc)):<EOL><INDENT>return tarfile.open(str(loc), "<STR_LIT>")<EOL><DEDENT>elif loc.parts[-<NUM_LIT:1>].endswith("<STR_LIT>"):<EOL><INDENT>return (line.decode("<STR_LIT:utf8>") for line in gzip.open(str(loc), "<STR_LIT:r>"))<EOL><DEDENT>elif loc.parts[-<NUM_LIT:1>].endswith("<STR_LIT>"):<EOL><INDENT>zip_file = zipfile.ZipFile(str(loc))<EOL>names = zip_file.namelist()<EOL>file_ = zip_file.open(names[<NUM_LIT:0>])<EOL>return (line.decode("<STR_LIT:utf8>") for line in file_)<EOL><DEDENT>else:<EOL><INDENT>return loc.open("<STR_LIT:r>", encoding="<STR_LIT:utf8>")<EOL><DEDENT>
Handle .gz, .tar.gz or unzipped files