Bonjour,

J'ai une class pour rechercher des fichiers ou dossiers et j'ai un petit souci de lenteur dans la rapidité de mes traitements. J'utilise os.listdir ou os.walk suivant les options choisis par l'utilisateur mais dans le cadre d'un os.walk le temps de tri sur les résultats est bien trop important par rapport à l'os.walk en lui même (c'est visible au niveau du temps de traitement et au voyant d'activité du disque).

Voici comment cela se passe :

Dans un premier temps j'ai un Toplevel Tkinter (mais ce n'est pas le sujet) wiClassRechercher avec des Checkbutton pour définir des options de recherche ([img=http://img171.imageshack.us/img171/6343/interfce.th.png])
Après validation et d'après les StringVar je fais un wiClassRechercher.withdraw() et je crée un Toplevel wiResultRecherche que je withdraw aussi dans l'attente de mes résultats.

Donc pour en venir au traitement proprement dit je procède de cette manière :

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
                if Checkcourrant_Checkbutton_StringVar.get() == "yes":
                    listresult = []
                    listtmp = os.listdir(os.getcwd())
                    for items in listtmp:
                        CheminItem = os.path.join(os.getcwd(), items)
                        if CheminItem == CheminItem.replace('/.','/'):
                            if Checktous_Checkbutton_StringVar.get() == "yes" and items[-1] != '~':
                                listresult.append(CheminItem)
                            elif Checkfonly_Checkbutton_StringVar.get() == "yes" and items[-1] != '~' and os.path.isfile(CheminItem):
                                listresult.append(CheminItem)
                            elif Checkdonly_Checkbutton_StringVar.get() == "yes" and os.path.isdir(CheminItem):
                                listresult.append(CheminItem)
                elif Checkrepsous_Checkbutton_StringVar.get() == "yes":
                    listresult = []
                    for root, dirs, files in os.walk(os.getcwd()):
                        RalentRecherche += 1
                        if RalentRecherche == 100:
                            IndRecherche += 1
                            if IndRecherche == 190: IndRecherche = 0
                            PgBarre.Maj_Recherche(IndRecherche)
                            RalentRecherche = 0
                        TriResult(root, dirs, files)
                elif Checkperso_Checkbutton_StringVar.get() == "yes":
                    listresult = []
                    for root, dirs, files in os.walk(os.environ['HOME']):
                        RalentRecherche += 1
                        if RalentRecherche == 100:
                            IndRecherche += 1
                            if IndRecherche == 190: IndRecherche = 0
                            PgBarre.Maj_Recherche(IndRecherche)
                            RalentRecherche = 0
                        TriResult(root, dirs, files)
                for items in listresult:
                    RalentRecherche += 1
                    if RalentRecherche == 100:
                        IndRecherche += 1
                        if IndRecherche == 190: IndRecherche = 0
                        PgBarre.Maj_Recherche(IndRecherche)
                        RalentRecherche = 0
                    if Checkcomplet_Checkbutton_StringVar.get() == "yes":
                        if Checkcase_Checkbutton_StringVar.get() == "yes" and str(os.path.basename(items)) == str(Entry_Recherche_StringVar.get()):
                            result.append(items)
                        elif Checkcase_Checkbutton_StringVar.get() == "no" and str(os.path.basename(items)).lower() == str(Entry_Recherche_StringVar.get()).lower():
                            result.append(items)
                    else:
                        if len(str(Entry_Recherche_StringVar.get())) < len(str(os.path.basename(items))) + 1:
                            Val_Ind = 0
                            while (Val_Ind < len(str(os.path.basename(items)))-len(str(Entry_Recherche_StringVar.get())) + 1):
                                if Checkcase_Checkbutton_StringVar.get() == "yes":
                                    if str(os.path.basename(items))[Val_Ind:len(str(Entry_Recherche_StringVar.get()))+Val_Ind] == str(Entry_Recherche_StringVar.get()):
                                        result.append(items)
                                        break
                                else:
                                    if str(os.path.basename(items))[Val_Ind:len(str(Entry_Recherche_StringVar.get()))+Val_Ind].lower() == str(Entry_Recherche_StringVar.get()).lower():
                                        result.append(items)
                                        break
                                Val_Ind += 1 
                for items in result:
                    RalentRecherche += 1
                    if RalentRecherche == 100:
                        IndRecherche += 1
                        if IndRecherche == 190: IndRecherche = 0
                        PgBarre.Maj_Recherche(IndRecherche)
                        RalentRecherche = 0
                    ListResultRecherche.insert(END, items)
                    if os.path.isfile(items):
                        ListResultRecherche.itemconfig(ListResultRecherche.size() - 1, fg='#2a7aff', selectforeground = '#2a7aff')
                wiResultRecherche.deiconify()
                wiClassRechercher.destroy()
Ce qui me fais déjà un traitement de trois listes plus le Def TriResult que voici... :

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
            def TriResult(root, dirs, files):
                if Checkfonly_Checkbutton_StringVar.get() == "yes":
                    for items in files:
                        CheminFiles = os.path.join(root, items)
                        if CheminFiles == CheminFiles.replace('/.','/') and items[-1:] != '~':
                            listresult.append(CheminFiles)
                elif Checkdonly_Checkbutton_StringVar.get() == "yes":
                    for items in dirs:
                        CheminDir = os.path.join(root, items)
                        if CheminDir == CheminDir.replace('/.','/'):
                            listresult.append(CheminDir)
                else:
                    for items in files:
                        CheminFiles = os.path.join(root, items)
                        if CheminFiles == CheminFiles.replace('/.','/') and items[-1:] != '~':
                            listresult.append(CheminFiles)
                    for items in dirs:
                        CheminDir = os.path.join(root, items)
                        if CheminDir == CheminDir.replace('/.','/'):
                            listresult.append(CheminDir)
Pour ce qui est de PgBarre ([img=http://img406.imageshack.us/img406/1214/captureah.th.png]) j'ai mis cela en place le temps de trouver une solution pour accélérer tout cela (et faire patienter l'utilisateur). En voici toutefois le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
PgBarre = BarreProgress(IndRecherche, lanceur)
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
class BarreProgress(Toplevel):
 
    def __init__(self, IndRecherche, lanceur):
        Toplevel.__init__(self, IndRecherche)
        self.withdraw()
        global TmpAttendCanvas
        global BarreBleu
        global wiTmpAttend
        if lanceur == 'Zip': # Non utiliser pour le moment.
            TextBarre = u'Archivage en cours...'
        elif lanceur == 'Recherche':
            TextBarre = 'Recherche en cours...'
        elif lanceur == 'Tempo':
            TextBarre = u'Vérification en cours...'
        wiTmpAttend = Toplevel()
        wiTmpAttend.overrideredirect(1)
        wt = wiTmpAttend.winfo_screenwidth()
        ht = wiTmpAttend.winfo_screenheight()
        wiTmpAttend.geometry("%dx%d+%d+%d" % (200,90, (wt-200)/2, (ht-90)/2 ) )
        BordureTmpAttend = Frame(wiTmpAttend, bd=2, relief=GROOVE)
        BordureTmpAttend.place(x=1, y=1, relwidth = 1, relheight = 1, bordermode="inside")
        Label(BordureTmpAttend, fg = "red", text = os.linesep + TextBarre + os.linesep).pack(side=TOP)
        TmpAttendCanvas = Canvas(BordureTmpAttend, height=20, width=180, bg='white', bd=2, relief=SUNKEN)
        TmpAttendCanvas.pack(side=TOP)
        BarreBleu = TmpAttendCanvas.create_rectangle((IndRecherche, 0, 20 + IndRecherche, 20), outline='#2a7aff',fill='#2a7aff', width=2)
 
    def Maj_Recherche(self, IndRecherche):
        TmpAttendCanvas.coords(BarreBleu, (IndRecherche, 0, 20 + IndRecherche, 20))
        TmpAttendCanvas.update_idletasks()
 
    def BarreQuit(self):
        wiTmpAttend.destroy()
Et pour finir j'affiche donc mon Toplevel wiResultRecherche ([img=http://img30.imageshack.us/img30/2971/capture1u.th.png]).

Comment réduire tous ses traitements ? En fait j'ai besoin des résultats du tri de la liste précédente pour la filtrer sur la suivante donc je cale sur la manière de procéder...

Pour la partie :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
                else:
                    for items in files:
                        CheminFiles = os.path.join(root, items)
                        if CheminFiles == CheminFiles.replace('/.','/') and items[-1:] != '~':
                            listresult.append(CheminFiles)
                    for items in dirs:
                        CheminDir = os.path.join(root, items)
                        if CheminDir == CheminDir.replace('/.','/'):
                            listresult.append(CheminDir)
j'ai bien penser aux threads, un pour files et un pour dirs, mais est il raisonnable de faire cela sur la même liste ? (Je n'ai pas l'habitude des threads)

Merci