Filename changer


/ Published in: Python
Save to your folder(s)

This code changes the part of the file name in accordance with the pattern


Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2.  
  3. import os, sys
  4.  
  5.  
  6.  
  7.  
  8. def migrate(old_name, new_name, dir_name):
  9. renamed=0
  10. notrenamed=0
  11. for walk in os.walk(dir_name):
  12. filelist=walk[2]
  13. if filelist:
  14. for file in filelist:
  15. oldfilename=walk[0]+file
  16. newfilename=oldfilename.replace(old_name, new_name)
  17. #os.rename(oldfilename, newfilename)
  18. print "OLD_WAS: ", oldfilename
  19. print "NEW_IS: ", newfilename
  20. print 200*"-"
  21.  
  22. if __name__=="__main__":
  23. if len(sys.argv)<4:
  24. print "Error"
  25. sys.exit()
  26. old=sys.argv[1]
  27. new=sys.argv[2]
  28. dirname=sys.argv[3]
  29. if dirname[-1]!="/":
  30. dirname=dirname+"/"
  31. migrate(old, new, dirname)

URL: python_renamer

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.