Skip to content

Instantly share code, notes, and snippets.

@NanoDano
Created March 4, 2023 21:07
Show Gist options
  • Save NanoDano/625f6f33d1d6a3c8aa4384f6c2293145 to your computer and use it in GitHub Desktop.
Save NanoDano/625f6f33d1d6a3c8aa4384f6c2293145 to your computer and use it in GitHub Desktop.
Rename all files recursively
# Remove a prefix from all files in recursive subdirs
import os
PREFIX_TO_REMOVE = "Something - "
ROOT_DIR = "."
for (dirpath, dirs, files) in os.walk(ROOT_DIR):
for filename in files:
# print(f'{filename} {dirpath} {dirs}')
if filename.startswith(PREFIX_TO_REMOVE):
new_filename = filename.removeprefix(PREFIX_TO_REMOVE)
relative_path = os.path.join(dirpath, filename)
print(relative_path)
new_path = os.path.join(dirpath, new_filename)
print(new_path)
os.rename(relative_path, new_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment