Created
December 26, 2018 08:48
-
-
Save fukasawah/7468adb1fe68706a26a25944fc8694ed to your computer and use it in GitHub Desktop.
Jupyter Notebookのoutputsとexecution_countを消す(上書き+バックアップ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Original: https://gist.github.com/damianavila/5305869 | |
""" | |
import sys | |
import io | |
import os | |
import glob | |
from datetime import datetime | |
from os.path import dirname, basename | |
from shutil import move, copytree | |
from nbformat import read, write, NO_CONVERT | |
if __name__ == '__main__': | |
TIMESTAMP=datetime.now().strftime('%Y%m%d-%H%M%S') | |
nb_paths = glob.glob('books/**/*.ipynb', recursive=True) | |
for nb_path in nb_paths: | |
original_dir = dirname(nb_path) | |
original_name = basename(nb_path) | |
with io.open(nb_path, 'r', encoding='utf8') as f: | |
nb = read(f, NO_CONVERT) | |
# backup | |
backup_dir = '/'.join(['backup', TIMESTAMP, original_dir]) | |
os.makedirs(backup_dir, exist_ok=True) | |
move(nb_path, '{}/{}'.format(backup_dir, original_name)) | |
for cell in nb.cells: | |
if cell.cell_type == 'code': | |
cell.outputs = [] | |
cell.execution_count = None | |
with io.open(nb_path, 'w', encoding='utf8') as f: | |
write(nb, f, NO_CONVERT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment