Skip to content

Instantly share code, notes, and snippets.

@john-sandall
Created October 14, 2021 21:01
Show Gist options
  • Save john-sandall/ed6bbb8ba749bcc162bc73118709dc8f to your computer and use it in GitHub Desktop.
Save john-sandall/ed6bbb8ba749bcc162bc73118709dc8f to your computer and use it in GitHub Desktop.
Rename files & folders in current directory
# In IPython, you can change the current working directory using
# %cd /path/to/some/directory/
import os
from pathlib import Path
# Directory containing folders you want to rename
here = Path(".") # "." means "the current directory"
# Loop through all the folders & files in this directory
for current_name in os.listdir():
# Remove spaces & lowercase
new_name = current_name.replace(" ", "_").lower()
# Rename each file/folder
os.rename(here / current_name, here / new_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment