Skip to content

Instantly share code, notes, and snippets.

@johnscancella
Created February 24, 2017 14:48
Show Gist options
  • Save johnscancella/3bb03f68ad16cb1f87e5ed32db518dcf to your computer and use it in GitHub Desktop.
Save johnscancella/3bb03f68ad16cb1f87e5ed32db518dcf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
if len(sys.argv) < 2:
print("must specify a folder to run against for rename")
sys.exit()
#lists so they maintain order
src = []
dest = []
for root, dirs, files in os.walk(sys.argv[1]):
#print("root is %s" % root)
for directory in dirs:
#print("directory is %s" % directory)
#remove _ from folder name
folder = directory.replace('_', '')
#add 'g' to the beginning of the folder name
folder = 'g' + folder
#print("folder name would be: %s" % folder)
src.append(os.path.join(root, directory))
dest.append(os.path.join(root, folder))
#rename the folders.
#Had to build a list first otherwise it will not properly continue into sub directories after rename
#we start with the most sub folder so that when we rename it, the other paths aren't broken
for i in reversed(range(0, len(src))):
print("renaming %s to %s" %(src[i], dest[i]))
os.rename(src[i], dest[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment