Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf8 -*-
- # Copyright © 2022 Benoît Boudaud <https://miamondo.org/contact>
- # This program is free software: you can redistribute it and/or modify it under the terms
- # of the GNU General Public License as published by the Free Software Foundation, either
- # version 3 of the License, or any later version. This program is distributed in the hope
- # that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- # for more details. You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>
- # This script renames folders and files. Letters with diacritical marks in French and in
- # German are replaced with letters without diacritical marks.
- #
- # 1. By default, this script takes only one parameter, which is the absolute path of the
- # directory you want to scan through. In that case,if there is only one parameter, all
- # the letters in the big dictionary keys will be replaced. For example:
- # ~/rename_files.py /home/$USER/Images
- # 2. If you choose to replace only one letter, you have to give two other parameters.
- # For example:
- # ~/rename_files.py /home/$USER/Images é e (it will replace é with e)
- # MODULES
- import os
- import sys
- import shutil
- # DICTIONARIES
- try: # If the script is run with a 2nd and a 3rd parameter:
- Letters = {sys.argv[2]: sys.argv[3]}
- except IndexError: # If the script is run without a second and a third paremeter:
- Letters = {
- " ": "_", "'": "_", "à": "a", "À": "A", "â": "a", "Â": "A", "ä": "ae",
- "Ä": "AE", "æ": "ae", "Æ": "AE", "ç": "c", "Ç": "C", "é": "e", "É": "E",
- "è": "e", "È": "E", "ê": "e", "Ê": "E", "ë": "e", "Ë": "E", "î": "i",
- "Î": "I", "ï": "i", "Ï": "I", "ô": "o", "Ô": "O", "ö": "oe", "Ö": "OE",
- "œ": "oe", "Œ": "OE", "ù": "u", "Ù": "U", "û": "u", "Û": "U", "ü": "ue",
- "Ü": "UE", "ÿ": "y", "Ÿ": "Y"
- }
- # SCANNED DIRECTORY
- scanned_directory = sys.argv[1] # The directory that you want to scan through
- # PATHS CONTAINING LETTERS WITH DIACRITICAL MARKS
- for key, value in Letters.items():
- paths = [] # gathers every absolute paths containing letters with diacritical marks
- for root, dirs, files in os.walk(scanned_directory):
- if key in root and root not in paths:
- paths.append(root)
- for dir_ in dirs:
- if key in dir_ and not os.path.join(root, dir_) in paths:
- paths.append(os.path.join(root, dir_))
- for file_ in files:
- if key in file_ and not os.path.join(root, file_) in paths:
- paths.append(os.path.join(root, file_))
- # RENAMING PATHS WITH SHUTIL.MOVE()
- for i, p in enumerate(paths):
- if i < len(paths) - 1:
- paths[i+1:i+2] = [os.path.join(os.environ["HOME"], os.path.dirname(paths[i+1].replace(
- f'{os.environ["HOME"]}/', "").replace(key, value)), os.path.basename(paths[i+1]))]
- p2 = p.replace(f'{os.environ["HOME"]}/', "").replace(key, value)
- shutil.move(p, os.path.join(os.environ["HOME"], p2))
- scanned_directory = scanned_directory.replace(key, value)
Add Comment
Please, Sign In to add comment