extract a file from a bytes_io tar. Returns bytes

def extract_file_from_tar(bytes_io, expected_file):
    """extract a file from a bytes_io tar. Returns bytes"""
    with open('temp', 'wb+') as f:
        bytes_io.seek(0)
        shutil.copyfileobj(bytes_io, f, length=131072)
    tar = tarfile.open('temp', mode='r:gz')
    os.remove('temp')
    return tar.extractfile(expected_file).read()