当使用IPython的%%file
魔术将notebook单元格的内容写入当前工作目录中的文件时,有没有一种方法可以抑制在执行单元格时显示的Created file ...
信息文本?
有时以这种方式创建文件非常方便(例如,当使用Matlab内核时),但这对于版本控制来说是一个巨大的问题,我不希望我的本地文件系统的结构也出现在其他人工作的代码中。
发布于 2019-03-27 04:09:07
此函数的源代码
@cell_magic
def writefile(self, line, cell):
"""Write the contents of the cell to a file.
The file will be overwritten unless the -a (--append) flag is specified.
"""
args = magic_arguments.parse_argstring(self.writefile, line)
if re.match(r'^(\'.*\')|(".*")$', args.filename):
filename = os.path.expanduser(args.filename[1:-1])
else:
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)
File: /usr/local/lib/python3.6/dist-packages/IPython/core/magics/osm.py
https://stackoverflow.com/questions/55342529
复制相似问题