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', '--a |
|
701 | '-a', '--append', action='store_true', default=False, | |
702 | help='Open file for amending if it exists' |
|
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,19 +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 | For frontends that do not support stdin (Notebook), -f is implied. |
|
713 | The file will be overwritten unless the -a (--append) flag is specified. | |
713 | """ |
|
714 | """ | |
714 | args = magic_arguments.parse_argstring(self.file, line) |
|
715 | args = magic_arguments.parse_argstring(self.file, line) | |
715 | filename = os.path.expanduser(unquote_filename(args.filename)) |
|
716 | filename = os.path.expanduser(unquote_filename(args.filename)) | |
716 |
|
717 | |||
717 | if os.path.exists(filename): |
|
718 | if os.path.exists(filename): | |
718 |
if args.a |
|
719 | if args.append: | |
719 |
print "A |
|
720 | print "Appending to %s" % filename | |
720 | else: |
|
721 | else: | |
721 | print "Overwriting %s" % filename |
|
722 | print "Overwriting %s" % filename | |
722 | else: |
|
723 | else: | |
723 | print "Writing %s" % filename |
|
724 | print "Writing %s" % filename | |
724 |
|
725 | |||
725 |
mode = 'a' if args.a |
|
726 | mode = 'a' if args.append else 'w' | |
726 | with io.open(filename, mode, encoding='utf-8') as f: |
|
727 | with io.open(filename, mode, encoding='utf-8') as f: | |
727 | f.write(cell) |
|
728 | f.write(cell) |
General Comments 0
You need to be logged in to leave comments.
Login now