Created
January 16, 2017 07:38
-
-
Save chamoda/72d95cfc3b9c4da6cc89d1e6b73357e8 to your computer and use it in GitHub Desktop.
Script to convert CamelCase filenames to snake_case
This file contains 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
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