def write(sim, name, attributes):
    """
    Write generic attributes of simulation and system to a file.

    `name` is a tag appended to `sim.base_path` to define the output
    file path.

    `attributes` must be a list of valid properties of the Simulation
    instance `sim` or of its System instance `sim.system`.
    """
    f = sim.output_path + '.' + name
    if sim.current_step == 0:
        with open(f, 'w') as fh:
            fh.write('# columns: %s\n' % ', '.join(attributes))
    else:
        values = []
        for attr in attributes:
            level = len(attr.split('.'))
            if level == 1:
                values.append(getattr(sim, attr))
            elif level == 2:
                system_attr = attr.split('.')[-1]
                if attr.startswith('system'):
                    values.append(getattr(sim.system, system_attr))
            else:
                raise ValueError('attribute is too deep')
        fmt = '%s ' * len(attributes) + '\n'
        with open(f, 'a') as fh:
            fh.write(fmt % tuple(values))
