def write_file(filepath, append=False):
    """A transform that writes the incoming objects, one per line, to
    the filepath.

    Use like chain.transform(transform_write_lines_to_filepath(the_filepath))
    If append == True, append to the file instead of overwriting it.
    """
    mode = 'a' if append else 'w'

    def write_lines_to_filepath(in_iterator):
        with open(filepath, mode) as output:
            for x in in_iterator:
                output.write(x)
                output.write('\n')
                yield x

    return write_lines_to_filepath