0

im trying to replace all dots (and accents, but's already done) in filename with underscore except extension dot of course. I saw a lot of solutions but mainly with bash. Cant find for python. Probably I should use regex but dont have much experience here. Below my code:

path2 = 'xx'
dicto = {"ą":"a", 
         "ś":"s",
         "ę":"e",
         "ć":"c",
         "ż":"z",
         "ź":"z",
         "ó":"o",
         "ł":"l",
         "ń":"n"}

def find_replace(string, dictionary):
    for item in string:
        if item in dictionary.keys():
            string = string.replace(item, dictionary[item])
    return string

def change_name(path=path2):
    for root, dirs, files in os.walk(path):
        for filename in files:
            if not filename.startswith('~'):
                os.rename(os.path.join(root,filename), os.path.join(root,find_replace(filename, dicto)))


change_name()

Rafał
  • 51
  • 10
  • You can have a look at the [`unidecode`](https://pypi.org/project/Unidecode/) module for converting your non-Latin unicode letters to plain ASCII. – S3DEV Feb 20 '20 at 12:27

3 Answers3

1

There are a number of existing answers here to remove diacritics from Unicode text in python. There is a unidecode library for python3 which does exactly what you need.

Removing diacritical marks using Python

What is the best way to remove accents in a Python unicode string?

benwiggy
  • 1,440
  • 17
  • 35
  • 1
    @Rafał - This will make your diacritics replacement easier and more robust. Albeit, the dot issue is still outstanding. **Use regex**. – S3DEV Feb 20 '20 at 12:39
  • @S3DEV I cant' use unicode, because I cant install new packages. Im asking for regex solution. How should I use it – Rafał Feb 20 '20 at 13:06
  • @Rafał - Understood. May I suggest updating your question with this requirement, as 2/3 of your answers recommend using the `unidecode` module, as we didn't know you can't install new packages. Aside, this is an [online regex tool](https://regexr.com/) I've found **very** useful. And [here is a Python regex tutorial](https://www.w3schools.com/python/python_regex.asp). – S3DEV Feb 20 '20 at 13:10
1

Just use Unidecode

import unidecode

print(unidecode.unidecode('ąśęćżźółń))

Output:

aseczzoln
Joan Lara
  • 1,362
  • 8
  • 15
0

Check out os.path.splitext and replace dots only in the first part of the filename.

haemka
  • 130
  • 2
  • 8