##// END OF EJS Templates
Even better docstrings for %%file...
MinRK -
Show More
@@ -698,8 +698,9 b' class OSMagics(Magics):'
698
698
699 @magic_arguments.magic_arguments()
699 @magic_arguments.magic_arguments()
700 @magic_arguments.argument(
700 @magic_arguments.argument(
701 '-a', '--amend', action='store_true', default=False,
701 '-a', '--append', action='store_true', default=False,
702 help='Open file for amending if it exists, instead of overwriting.'
702 help='Append contents of the cell to an existing file. '
703 'The file will be created if it does not exist.'
703 )
704 )
704 @magic_arguments.argument(
705 @magic_arguments.argument(
705 'filename', type=unicode,
706 'filename', type=unicode,
@@ -709,20 +710,19 b' class OSMagics(Magics):'
709 def file(self, line, cell):
710 def file(self, line, cell):
710 """Write the contents of the cell to a file.
711 """Write the contents of the cell to a file.
711
712
712 The file will be overwritten if it exists, unless the `-a` flag is passed,
713 The file will be overwritten unless the -a (--append) flag is specified.
713 in which case the file will be amended.
714 """
714 """
715 args = magic_arguments.parse_argstring(self.file, line)
715 args = magic_arguments.parse_argstring(self.file, line)
716 filename = os.path.expanduser(unquote_filename(args.filename))
716 filename = os.path.expanduser(unquote_filename(args.filename))
717
717
718 if os.path.exists(filename):
718 if os.path.exists(filename):
719 if args.amend:
719 if args.append:
720 print "Amending to %s" % filename
720 print "Appending to %s" % filename
721 else:
721 else:
722 print "Overwriting %s" % filename
722 print "Overwriting %s" % filename
723 else:
723 else:
724 print "Writing %s" % filename
724 print "Writing %s" % filename
725
725
726 mode = 'a' if args.amend else 'w'
726 mode = 'a' if args.append else 'w'
727 with io.open(filename, mode, encoding='utf-8') as f:
727 with io.open(filename, mode, encoding='utf-8') as f:
728 f.write(cell)
728 f.write(cell)
General Comments 0
You need to be logged in to leave comments. Login now