Skip to content

Instantly share code, notes, and snippets.

@JupyterJones
Created February 15, 2023 03:11
Show Gist options
  • Save JupyterJones/d5cdcb205db6740275cd712d5645e28a to your computer and use it in GitHub Desktop.
Save JupyterJones/d5cdcb205db6740275cd712d5645e28a to your computer and use it in GitHub Desktop.
Locate and copy all jupyter notebooks in one directory renaming duplicate filenames
#Locate and Copy all Jupyter notebooks in one directory
import os
import shutil
src_dir = '/home/jack' # Replace this with the root directory where you want to search for Jupyter notebooks
dst_dir = '/home/jack/Desktop/HDD500/notebooks/' # Replace this with the destination directory where you want to copy the notebooks
notebooks = {} # Dictionary to keep track of notebook names and sequence numbers
for root, dirs, files in os.walk(src_dir):
for file in files:
if file.endswith('.ipynb'):
src_path = os.path.join(root, file)
dst_path = os.path.join(dst_dir, file)
if file in notebooks:
# Increment sequence number for duplicate file names
notebooks[file] += 1
name, ext = os.path.splitext(file)
new_name = f"{name}_{notebooks[file]}{ext}"
dst_path = os.path.join(dst_dir, new_name)
else:
notebooks[file] = 0
try:
shutil.copy2(src_path, dst_path)
except PermissionError:
# Skip directories that we don't have permission to read
continue
print("Done copying Jupyter notebooks!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment