Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import sys
  5.  
  6. if len(sys.argv) < 2:
  7. print("must specify a folder to run against for rename")
  8. sys.exit()
  9.  
  10. #lists so they maintain order
  11. src = []
  12. dest = []
  13.  
  14. for root, dirs, files in os.walk(sys.argv[1]):
  15. #print("root is %s" % root)
  16. for directory in dirs:
  17. #print("directory is %s" % directory)
  18. #remove _ from folder name
  19. folder = directory.replace('_', '')
  20. #add 'g' to the beginning of the folder name
  21. folder = 'g' + folder
  22. #print("folder name would be: %s" % folder)
  23. src.append(os.path.join(root, directory))
  24. dest.append(os.path.join(root, folder))
  25.  
  26. #rename the folders.
  27. #Had to build a list first otherwise it will not properly continue into sub directories after rename
  28. #we start with the most sub folder so that when we rename it, the other paths aren't broken
  29. for i in reversed(range(0, len(src))):
  30. print("renaming %s to %s" %(src[i], dest[i]))
  31. os.rename(src[i], dest[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement