27 lines
854 B
Python
27 lines
854 B
Python
|
import os
|
||
|
import nbformat
|
||
|
|
||
|
def clear_notebook_output(notebook_path):
|
||
|
with open(notebook_path, 'r', encoding='utf-8') as f:
|
||
|
nb = nbformat.read(f, as_version=4)
|
||
|
|
||
|
for cell in nb.cells:
|
||
|
if cell.cell_type == 'code':
|
||
|
cell.outputs = []
|
||
|
cell.execution_count = None
|
||
|
|
||
|
with open(notebook_path, 'w', encoding='utf-8') as f:
|
||
|
nbformat.write(nb, f)
|
||
|
print(f'Cleared output for {notebook_path}')
|
||
|
|
||
|
def walk_and_clear_outputs(base_dir):
|
||
|
for root, dirs, files in os.walk(base_dir):
|
||
|
for file in files:
|
||
|
if file.endswith('.ipynb'):
|
||
|
notebook_path = os.path.join(root, file)
|
||
|
clear_notebook_output(notebook_path)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
base_dir = '.' # Change this to the directory you want to start from
|
||
|
walk_and_clear_outputs(base_dir)
|