miamondo

rename_files.py

Jan 21st, 2022 (edited)
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.33 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf8 -*-
  3.  
  4. # Copyright © 2022 Benoît Boudaud <https://miamondo.org/contact>
  5. # This program is free software: you can redistribute it and/or modify it under the terms
  6. # of the GNU General Public License as published by the Free Software Foundation, either
  7. # version 3 of the License, or any later version. This program is distributed in the hope
  8. # that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10. # for more details. You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>
  12.  
  13.  
  14. # This script renames folders and files. Letters with diacritical marks in French and in
  15. # German are replaced with letters without diacritical marks.
  16. #
  17. #  1. By default, this script takes only one parameter, which is the absolute path of the
  18. #     directory you want to scan through. In that case,if there is only one parameter, all
  19. #     the letters in the big dictionary keys will be replaced. For example:
  20. #           ~/rename_files.py /home/$USER/Images
  21. #  2. If you choose to replace only one letter, you have to give two other parameters.
  22. #     For example:
  23. #           ~/rename_files.py /home/$USER/Images é e (it will replace é with e)
  24.  
  25.  
  26. # MODULES
  27.  
  28. import os
  29. import sys
  30. import shutil
  31.  
  32. # DICTIONARIES
  33.  
  34. try:    # If the script is run with a 2nd and a 3rd parameter:
  35.     Letters = {sys.argv[2]: sys.argv[3]}
  36. except IndexError:    # If the script is run without a second and a third paremeter:
  37.     Letters = {
  38.         " ": "_", "'": "_", "à": "a", "À": "A", "â": "a", "Â": "A", "ä": "ae",
  39.         "Ä": "AE", "æ": "ae", "Æ": "AE", "ç": "c", "Ç": "C", "é": "e", "É": "E",
  40.         "è": "e", "È": "E", "ê": "e", "Ê": "E", "ë": "e", "Ë": "E", "î": "i",
  41.         "Î": "I", "ï": "i", "Ï": "I", "ô": "o", "Ô": "O", "ö": "oe", "Ö": "OE",
  42.         "œ": "oe", "Œ": "OE", "ù": "u", "Ù": "U", "û": "u", "Û": "U", "ü": "ue",
  43.         "Ü": "UE", "ÿ": "y", "Ÿ": "Y"
  44.         }
  45.  
  46. # SCANNED DIRECTORY
  47.  
  48. scanned_directory = sys.argv[1]    # The directory that you want to scan through
  49.  
  50. # PATHS CONTAINING LETTERS WITH DIACRITICAL MARKS
  51.  
  52. for key, value in Letters.items():
  53.     paths = []    # gathers every absolute paths containing letters with diacritical marks
  54.     for root, dirs, files in os.walk(scanned_directory):
  55.         if key in root and root not in paths:
  56.             paths.append(root)
  57.         for dir_ in dirs:
  58.             if key in dir_ and not os.path.join(root, dir_) in paths:
  59.                 paths.append(os.path.join(root, dir_))
  60.         for file_ in files:
  61.             if key in file_ and not os.path.join(root, file_) in paths:
  62.                 paths.append(os.path.join(root, file_))
  63.  
  64. # RENAMING PATHS WITH SHUTIL.MOVE()
  65.  
  66.     for i, p in enumerate(paths):
  67.         if i < len(paths) - 1:
  68.             paths[i+1:i+2] = [os.path.join(os.environ["HOME"], os.path.dirname(paths[i+1].replace(
  69.                 f'{os.environ["HOME"]}/', "").replace(key, value)), os.path.basename(paths[i+1]))]
  70.         p2 = p.replace(f'{os.environ["HOME"]}/', "").replace(key, value)
  71.         shutil.move(p, os.path.join(os.environ["HOME"], p2))
  72.     scanned_directory = scanned_directory.replace(key, value)
  73.  
Add Comment
Please, Sign In to add comment