def write_out_file(contents, filename, mode='w'):
    """Write contents to outfile directly.
    Default mode is truncate/create new file; pass mode='a' if append to existing.
    """
    try:
        with open(filename, mode) as f:
            f.write(contents)
    except Exception as e:
        print(f'Error: {e}')
        return None
    else:
        return filename