Skip to content

Instantly share code, notes, and snippets.

@JupyterJones
Created February 15, 2023 03:44
Show Gist options
  • Save JupyterJones/3ff24ec52e7311fab6e3e91bee2ea145 to your computer and use it in GitHub Desktop.
Save JupyterJones/3ff24ec52e7311fab6e3e91bee2ea145 to your computer and use it in GitHub Desktop.
replace a string in a jupyter notebook get rid of any passwords
import os
import re
search_dir = '/home/jack/Desktop/HDD500/notebooks' # Replace this with the directory where you want to search for Jupyter notebooks
search_string = 'password' # Replace this with the string you want to search for
replace_string = 'XXXnopassword' # Replace this with the string you want to replace the search string with
for file in os.listdir(search_dir):
if file.endswith('.ipynb'):
with open(os.path.join(search_dir, file), 'r', encoding='utf-8') as nb_file:
nb_content = nb_file.read()
nb_content = re.sub(search_string, replace_string, nb_content)
with open(os.path.join(search_dir, file), 'w', encoding='utf-8') as nb_file:
nb_file.write(nb_content)
print(f"Done searching and replacing '{search_string}' with '{replace_string}'.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment