Skip to content

Instantly share code, notes, and snippets.

@chamoda
Created January 16, 2017 07:38
Show Gist options
  • Save chamoda/72d95cfc3b9c4da6cc89d1e6b73357e8 to your computer and use it in GitHub Desktop.
Save chamoda/72d95cfc3b9c4da6cc89d1e6b73357e8 to your computer and use it in GitHub Desktop.
Script to convert CamelCase filenames to snake_case
import os
import re
files = [f for f in os.listdir('.') if os.path.isfile(f)]
def convert(name):
s = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s).lower()
for file in files:
print(convert(file))
if file != 'rename.py':
os.rename(file, convert(file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment