Bonjour, j'ai écrit un programme qui tri automatiquement la musique dans des dossiers en fonction du nom de l'artiste et de l'album, étant mon premier vrai programme, j'aimerais savoir si mon code est optimisé ou s'il y a des erreurs qui passent inaperçues.
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
import mutagen
import os
import glob
 
#List all mp3 files in directory
def list_all_mp3():
    mp3_list = glob.iglob('*.mp3', recursive=False)
 
    return mp3_list
 
#Read the tag of the mp3 file
def get_mp3tag(file_name):
    audio = mutagen.File(file_name)
    album = str(audio['TALB'].text).strip("['']")
    artist = str(audio['TPE1'].text).strip("['']")
    return [artist, album]
 
#Verify if folder exist else create the folder
def create_folder(artist, album):
    if os.path.isdir(artist) is False:
        os.mkdir(artist)
        os.mkdir(artist+"/"+ album)
    elif os.path.isdir(artist+"/"+ album) is False:
        os.mkdir(artist+"/"+ album)
    else:
        print("The directory has been already created")
 
#Move the music to the folder
def move_mp3file(file_name, artist, album):
    if os.path.exists(artist + "/"+ album + "/" + file_name) is False:
        os.rename(file_name, artist + "/"+ album + "/" + file_name)
    else:
        print("This music is already in the directory")
 
def main():
    mp3_list = list_all_mp3()
 
    for mp3 in mp3_list:
        artist_album = get_mp3tag(mp3)
        artist = str(artist_album[0]).rstrip().translate({ord(c): None for c in '?!@#$:'})
        album = str(artist_album[1]).rstrip().translate({ord(c): None for c in '?!@#$:'})
        create_folder(artist, album)
        move_mp3file(mp3, artist, album)
 
if __name__ == "__main__":
    main()
Merci, d'avance