def write_file(content, filename, mode='wt', encoding=None):
    """
    Write ``content`` to disk at ``filename``. Files with appropriate extensions
    are compressed with gzip or bz2 automatically. Any intermediate folders
    not found on disk are automatically created.
    """
    _make_dirs(filename)
    _open = gzip.open if filename.endswith('.gz') \
        else bzip_open if filename.endswith('.bz2') \
        else io.open
    try:
        with _open(filename, mode=mode, encoding=encoding) as f:
            f.write(content)
    except __HOLE__:  # Py2's bz2.BZ2File doesn't accept `encoding` ...
        with _open(filename, mode=mode) as f:
            f.write(content)
dataset/ETHPy150Open chartbeat-labs/textacy/textacy/fileio/write.py/write_file