def METHOD_NAME(filename, prefix, tar_format, num_files):
    with tarfile.open(filename, "w|", format=tar_format) as tar:
        for i in range(num_files):
            # Create a file name and write random text to it
            filename = f"shard-{prefix}-file-{i}.txt"
            with open(filename, "w", encoding="utf-8") as text:
                text.write(random_string())
            # Add the file to the tarfile
            tar.add(filename)
            # Remove the file after adding it to the tarfile
            Path(filename).unlink()