简体   繁体   English

Jupyter:抑制 %%file 魔法输出

[英]Jupyter: suppress %%file magic output

When using IPython's %%file magic to write the content of a notebook cell to a file in the current working directory, is there a way to suppress the Created file ... info text displayed on execution of the cell?当使用 IPython 的%%file魔法将笔记本单元格的内容写入当前工作目录中的文件时,有没有办法抑制单元格执行时显示的Created file ... info 文本?

Sometimes creating files in this way is super handy (for example when using a Matlab kernel) but this is a huge problem with respect to version control, I don't want the structure of my local filesystem to be present in code that others work on as well.有时以这种方式创建文件非常方便(例如在使用 Matlab 内核时),但这在版本控制方面是一个大问题,我不希望我的本地文件系统的结构出现在其他人使用的代码中以及。

source for this function此功能的来源

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM