我试图将多个文件的内容放入一个ipython笔记本中。我找到了一个将多个ipynb文件组合成一个脚本的脚本,这样可行,但是当我尝试添加代码也将自述文件放在那里时,它不起作用。我不知道问题是什么。这是我的代码。 (我先把它简化为自述文件。
它运行良好,但笔记本电脑不会打开。
import nbformat; import os; import re
merged = None; oneTime = False
folderToWalkThrough = os.path.join('.','05-Matplotlib')
for root, dirs, files in os.walk(folderToWalkThrough, topdown=True):
for name in files: #Only care about the files
if re.search('\.md', name):
with open (os.path.join(root,name)) as readme:
readmeLines = readme.read()
if re.search('\.ipynb', name): #Make sure its a ipython notebook file
with open(os.path.join(root, name) ) as f:
nb = nbformat.read(f, as_version=4)
if merged == None:
merged = nb
else:
if readmeLines:
newCell = nbformat.v4.new_markdown_cell(str(readmeLines))
merged.cells.extend(newCell)
print(type(newCell), ': ',newCell)
merged.cells.extend( nb.cells)
newMergedCells = nbformat.v4.new_notebook()
newMergedCells.metadata.name = 'Test'
nbformat.write(merged, '{0}.ipynb'.format(newMergedCells.metadata.name))