def gzip_open_text(path, encoding=None):
if encoding is None:<EOL><INDENT>encoding = sys.getdefaultencoding()<EOL><DEDENT>assert os.path.isfile(path)<EOL>is_compressed = False<EOL>try:<EOL><INDENT>gzip.open(path, mode='<STR_LIT:rb>').read(<NUM_LIT:1>)<EOL><DEDENT>except IOError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>is_compressed = True<EOL><DEDENT>if is_compressed:<EOL><INDENT>if six.PY2:<EOL><INDENT>import codecs<EOL>zf = gzip.open(path, '<STR_LIT:rb>')<EOL>reader = codecs.getreader(encoding)<EOL>fh = reader(zf)<EOL><DEDENT>else:<EOL><INDENT>fh = gzip.open(path, mode='<STR_LIT>', encoding=encoding)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fh = open(path, mode='<STR_LIT:r>', encoding=encoding)<EOL><DEDENT>return fh<EOL>
Opens a plain-text file that may be gzip'ed.

    Parameters
    ----------
    path : str
        The file.
    encoding : str, optional
        The encoding to use.

    Returns
    -------
    file-like
        A file-like object.

    Notes
    -----
    Generally, reading gzip'ed files with gzip.open is very slow, and it is
    preferable to pipe the file into the python script using ``gunzip -c``.
    The script then reads the file from stdin.