Guest User

Untitled

a guest
Feb 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import os, sys
  2. from os import listdir
  3. from os.path import isfile, join
  4.  
  5. def rename_all_files(rel_path='', name_style="img_{}"):
  6. """
  7. Rename all files in the folder under rel_path.
  8.  
  9. Keyword arguments:
  10. rel_path -- relative path to the folder (default 0.0)
  11. name_style -- file naming convention with incrementing integer (default "img_{}")
  12. """
  13. dir_path = os.path.abspath(os.getcwd())
  14. full_path = os.path.join(dir_path, rel_path)
  15.  
  16. onlyfiles = [f for f in listdir(full_path) if isfile(join(full_path, f))]
  17. i = 1
  18.  
  19. for file in onlyfiles:
  20. infile = os.path.join(full_path, file)
  21. outfile = name_style.format(i) + ".jpg"
  22.  
  23. if infile != outfile:
  24. os.rename(infile, os.path.join(full_path, outfile))
  25. i = i + 1
  26. else:
  27. print('Could not rename matching names {} and {}'.format(infile, outfile))
Add Comment
Please, Sign In to add comment