import re
import gzip


def open_possible_gzip(infile, flags="rt"):
    """
    Opens a file handle for a gzipped or non-zipped file
    :param infile: Path to file
    :param flags:
    :return: file handle
    """
    infile = str(infile)
    if re.search("\.gz$", infile):
        f = gzip.open(infile, flags)
    else:
        f = open(infile, flags)
    return f