Skip to content

Instantly share code, notes, and snippets.

@amdevine
Created October 4, 2023 19:12
Show Gist options
  • Save amdevine/f45194ed747e4d286b5e60fe01efccbd to your computer and use it in GitHub Desktop.
Save amdevine/f45194ed747e4d286b5e60fe01efccbd to your computer and use it in GitHub Desktop.
Bulk rename files in Python
# Adapted from Automate the Boring Stuff with Python
# https://automatetheboringstuff.com/2e/chapter10/
import shutil, os
from pathlib import Path
dir = Path.cwd() / 'data_directory'
for f in os.listdir(dir):
# Adjust newname logic to whatever is desired
newname = f.replace('oldprefix1_', '').replace('oldprefix2_', '')
shutil.move(dir / f, dir / newname)
print(f'{f} renamed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment