Bonjour,

Je débute en Python et j'ai une expérience en Vba. Je trouve les scripts Python plus économique en nombre de ligne qu'en VB/VBA. Bref, le script ci-dessous plante avec ce message (environnement: Windows 7 / Python 3.5 / Notepad++)

NPP_SAVE: F:\tutopy\rep_prod_test\folder_copy_file_a.py
CD: F:\tutopy\rep_prod_test\folder_copy_file_a.py
Current directory: F:\tutopy\rep_prod_test
python "F:\tutopy\rep_prod_test\folder_copy_file_a.py"
Process started >>>
Traceback (most recent call last):
File "F:\tutopy\rep_prod_test\folder_copy_file_a.py", line 26, in <module>
line = f.readline()
File "C:\Users\capsal\AppData\Local\Programs\Python\Python35\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 872: character maps to <undefined>
<<< Process finished. (Exit code 1)


Et le script concerné:

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
import os
import shutil
 
folderName = os.getcwd()
 
folderNameB = folderName + '_copy'
 
if not os.path.exists(folderNameB):
    os.makedirs(folderNameB)
else:
    print('Dossier existant')
    shutil.rmtree(folderNameB)
    os.makedirs(folderNameB)
 
fdest = os.path.dirname(os.path.dirname(folderName))
fdest = os.path.join(fdest, folderNameB)
 
for root, dirs, files in os.walk(folderName):
    for file in files:
        if file.endswith('.txt'):
            with open(file, 'r') as f:
                fdestZ = os.path.join(fdest, file)
                with open(fdestZ, 'w') as fc:
                    content = ""
                    while 1:
                        line = f.readline()
                        if not line: break
                        content += line
                        fc.write(content)
Vous allez me dire qu'il y a d'autres moyens de faire ce que ce script fait, cependant il (la copie ligne par ligne) répond à un besoin
Est-ce que c'est possible d'afficher le nom du fichier qui génère l'erreur?

Merci d'avance

Pascal