Problem : I have dozens of jupyter notebooks that all have some text that I need to replace.
Example
Each notebook contains a reference to an external file in directory foo
.
./
|- foo
| |- image.jpg
| |- ...
|- notebooks
|- file1.ipynb
|- file2.ipynb
|- file3.ipynb
|- ...
However, the foo
directory will soon be renamed to bar
, thereby orphaning the associated links in the notebooks. Thus, I will need to rename all the links in each notebook from the old foo
directory name to the new bar
name, eg foo/image.jpg
to bar/image.jpg
.
Question : Opening each one by one will take a lot of time. Is there an efficient and safe solution for batch editing text for multiple jupyter notebooks?
Approach
Since notebooks are essentially json files, I am considering a simple search and replace in VSCode. I've tried this on a test notebook and it seems to work, but I am not sure if this method will somehow corrupt the files. I appreciate any safe suggestions.
Are you on *nix? Then sed will work.
cd ./path/to/notebooks
sed -i 's/foo/bar/g' *.ipynb
or you can use vim argsadd
coupled with argsdo
ie.,
vim
:cd path/to/notebooks
:arg *.ipynb
:argdo %s/foo/bar/ge | update
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.