'Write the contents of the cell to a file.
The file will be overwritten unless the -a (--append) flag is specified.'

@magic_arguments.magic_arguments()
@magic_arguments.argument('-a', '--append', action='store_true', default=False, help='Append    contents    of    the    cell    to    an    existing    file.    The    file    will    be    created    if    it    does    not    exist.')
@magic_arguments.argument('filename', type=str, help='file    to    write')
@cell_magic
def writefile(self, line, cell):

    args = magic_arguments.parse_argstring(self.writefile, line)
    filename = os.path.expanduser(args.filename)
    if os.path.exists(filename):
        if args.append:
            print ('Appending    to    %s' % filename)
        else:
            print ('Overwriting    %s' % filename)
    else:
        print ('Writing    %s' % filename)
    mode = ('a' if args.append else 'w')
    with io.open(filename, mode, encoding='utf-8') as f:
        f.write(cell)
