Created
October 4, 2023 19:12
-
-
Save amdevine/f45194ed747e4d286b5e60fe01efccbd to your computer and use it in GitHub Desktop.
Bulk rename files in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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