Write the contents of the cell to a file. The file will be overwritten unless the -a (--append) flag is specified.
(self, line, cell)
| 834 | ) |
| 835 | @cell_magic |
| 836 | def writefile(self, line, cell): |
| 837 | """Write the contents of the cell to a file. |
| 838 | |
| 839 | The file will be overwritten unless the -a (--append) flag is specified. |
| 840 | """ |
| 841 | args = magic_arguments.parse_argstring(self.writefile, line) |
| 842 | if re.match(r'^(\'.*\')|(".*")$', args.filename): |
| 843 | filename = os.path.expanduser(args.filename[1:-1]) |
| 844 | else: |
| 845 | filename = os.path.expanduser(args.filename) |
| 846 | |
| 847 | if os.path.exists(filename): |
| 848 | if args.append: |
| 849 | print("Appending to %s" % filename) |
| 850 | else: |
| 851 | print("Overwriting %s" % filename) |
| 852 | else: |
| 853 | print("Writing %s" % filename) |
| 854 | |
| 855 | mode = 'a' if args.append else 'w' |
| 856 | with io.open(filename, mode, encoding='utf-8') as f: |
| 857 | f.write(cell) |
nothing calls this directly
no test coverage detected