Skip to content

Instantly share code, notes, and snippets.

@adityasatalkar
Created March 25, 2021 10:05
Show Gist options
  • Save adityasatalkar/da81eea0ae8bcee9265b2deba59c23ad to your computer and use it in GitHub Desktop.
Save adityasatalkar/da81eea0ae8bcee9265b2deba59c23ad to your computer and use it in GitHub Desktop.
List all sub-directories in a dir and rename them to lower case, while replacing spaces with "-"
import os
path = '/Users/username/Documents/example-folder/'
def replace():
directory_contents = os.listdir(path)
for name in directory_contents:
if os.path.isdir(path + name):
file_path = os.path.join(path, name)
new_name = os.path.join(path, name.lower().strip().replace(" ", "-"))
print(new_name)
os.rename(file_path, new_name)
replace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment