def file_smart_open(fname, mode='rb'):
    _, ext = os.path.splitext(fname)

    if ext == '.bz2':
        PY2 = sys.version_info[0] == 2
        if PY2:
            from bz2file import BZ2File
        else:
            from bz2 import BZ2File
        return make_closing(BZ2File)(fname, mode)

    if ext == '.gz':
        from gzip import GzipFile
        return make_closing(GzipFile)(fname, mode)

    return open(fname, mode)
Select a file from every depth of a file_tree. This implementation relies on:
dataset/ETHPy150Open piskvorky/smart_open/smart_open/smart_open_lib.py/file_smart_open--crossover with--dataset/ETHPy150Open CenterForOpenScience/osf.io/tests/test_registrations/test_archiver.py/select_files_from_tree