MCPcopy Create free account
hub / github.com/ipython/ipython / writefile

Method writefile

IPython/core/magics/osm.py:836–857  ·  view source on GitHub ↗

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

(self, line, cell)

Source from the content-addressed store, hash-verified

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)

Callers

nothing calls this directly

Calls 2

parse_argstringMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected