jupyter-notebook - Jupyter:抑制 %%file 魔术输出
问题描述
当使用 IPython 的%%file
魔法将笔记本单元格的内容写入当前工作目录中的文件时,有没有办法抑制Created file ...
单元格执行时显示的信息文本?
有时以这种方式创建文件非常方便(例如在使用 Matlab 内核时),但这对于版本控制来说是一个大问题,我不希望我的本地文件系统的结构出现在其他人工作的代码中也是。
解决方案
此功能的来源
@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
推荐阅读
- python - Postgres:同时使用准备好的语句和字符转义是否足以避免恶意用户输入攻击?
- ssh - 如何在 .ssh/config 中模拟 ssh 命令
- python - 如何检查脚本是否已经导入?
- java - 两个蓝牙连接服务操作处理程序
- hadoop - Yarn Apps API - 可见性保证?
- mysql - 与 node.js 一起使用时,实际的 mysql 包似乎有语法错误
- c# - 无法通过脚本控制后期处理
- angular - Bootstrap 崩溃在 Angular 9 中不起作用
- r - 为什么我不能在 nls() 中用数字列选择器替换名称列选择器?
- javascript - 更新矩阵 useState 挂钩状态变量的特定值