Bonjour,
voici un code qui me pose problème sous Python 3:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/env python
 
# Sources :
#    http://www.developpez.net/forums/d884109/autres-langages/python-zope/general-python/renommer-dossiers-sous-dossiers-fichiers/
#    http://python.jpvweb.com/mesrecettespython/renommer_mp3
#    http://python.developpez.com/faq/?page=Fichier#ContenuRepertoire
 
import glob
import os.path
 
dirToClean = "/Users/user/Music/toSort"
dirForStoring = "/Users/user/Music/namesCleaned"
 
i_start = len(dirToClean)
 
# The following variables have been built by  tools_build_variables.py
UGGLY_CHARACTERS = {'Á': 'A', 'À': 'A', 'Ã': 'A', 'Â': 'A', 'Å': 'A', 'Ä': 'A', 'Ç': 'C', 'Æ': 'AE', 'É': 'E', 'È': 'E', 'Ë': 'E', 'Ê': 'E', 'Í': 'I', 'Ì': 'I', 'Ï': 'I', 'Î': 'I', 'Ñ': 'N', 'œ': 'oe', 'Œ': 'OE', 'Õ': 'O', 'Ô': 'O', 'Ö': 'O', 'Ù': 'U', 'Û': 'U', 'Ú': 'U', 'Ü': 'U', 'á': 'a', 'à': 'a', 'ã': 'a', 'â': 'a', 'ä': 'a', 'ç': 'c', 'æ': 'ae', 'é': 'e', 'è': 'e', 'ë': 'e', 'ê': 'e', 'Ó': 'O', 'ï': 'i', 'î': 'i', 'ñ': 'n', 'Ò': 'O', 'ô': 'o', 'ö': 'o', 'ù': 'u', 'Ÿ': 'Y', 'û': 'u', 'ü': 'u', 'ÿ': 'y'}
CHARACTERS_ALLOWED = " -_,'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + os.sep
 
 
 
def cleanString(stringToClean):
    prettyString = ''
 
    for oneChar in stringToClean:
        if oneChar in UGGLY_CHARACTERS:
            prettyString += UGGLY_CHARACTERS[oneChar]
        elif oneChar in CHARACTERS_ALLOWED:
            prettyString += oneChar
        else:
            raise ValueError('Unknown character : ' + oneChar + '\nString build : ' + prettyString)
 
    return prettyString
 
 
def listdirectory(path):
    fichier=[]
    for root, dirs, files in os.walk(path):
        for oneObject in files:
            path = os.path.join(root, oneObject)[i_start:]
            print('path')
            print(path)
            print(cleanString(str(path)))
 
 
listdirectory(dirToClean)
Dans le dossier /Users/user/Music/toSort, j'ai le dossier Boby Làpointé avec dedans le fichier .DS_Store.

Dans une console j'obtiens :
path
/Boby Làpointé/.DS_Store
Traceback (most recent call last):
File "/Users/user/Documents/myWorld/python/pylonga/clean_files_names/renameFileAndDirectory.py", line 46, in <module>
listdirectory(dirToClean)
File "/Users/user/Documents/myWorld/python/pylonga/clean_files_names/renameFileAndDirectory.py", line 43, in listdirectory
print(cleanString(str(path)))
File "/Users/user/Documents/myWorld/python/pylonga/clean_files_names/renameFileAndDirectory.py", line 31, in cleanString
raise ValueError('Unknown character : ' + oneChar + '\nString build : ' + prettyString)
ValueError: Unknown character : ̀
String build : /Boby La


Il est à peu près certain que j'ai un problème d'encodage des chaînes donnant les "paths". Que faire ? Ce que je ne pige pas, c'est que la console me renvoie la chaîne qu'il faudrait analyser alors que la procédure de lecture caractère par caractère fait comme si on avait " à " à la place de " a ̀ ".