def save_to_multiple_files()

in psaw/psaw.py [0:0]


def save_to_multiple_files(things, output_template, writer, count,
                           verbose=False, dry_run=False):
    """
    Write things to a separate file per thing

    :param things: iterable
    :param output_template: str
        template should contain python str format codes, such as "{subreddit}.{id}.json"
    :param writer: Writer
    :param count: int
    :param verbose: bool
    :param dry_run: bool

    """
    if dry_run:
        progressbar = ut.DummyProgressBar(things)
    else:
        progressbar = click.progressbar(things, length=count)

    count = 0
    with progressbar as things:
        for thing in things:
            output_file = output_template.format(**thing.d_)
            p = Path(output_file)
            if not p.parent.exists():
                p.parent.mkdir(parents=True)

            if dry_run:
                click.echo("saving to: {}".format(output_file))
            else:
                try:
                    writer.open(output_file)
                    writer.header()
                    writer.write(thing.d_)
                    writer.footer()
                    count += 1
                finally:
                    writer.close()

    if verbose:
        click.echo("wrote {} items".format(count))