首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Jupyter:禁止%%文件魔术输出

Jupyter:禁止%%文件魔术输出
EN

Stack Overflow用户
提问于 2019-03-26 00:37:15
回答 1查看 76关注 0票数 1

当使用IPython的%%file魔术将notebook单元格的内容写入当前工作目录中的文件时,有没有一种方法可以抑制在执行单元格时显示的Created file ...信息文本?

有时以这种方式创建文件非常方便(例如,当使用Matlab内核时),但这对于版本控制来说是一个巨大的问题,我不希望我的本地文件系统的结构也出现在其他人工作的代码中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55342529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档