0

I made a simple script in python 3 that enters in some folders (all called "recup_dir.x", x stays for a number from 1 to 777) and searches for files containing "firefox" word. Since the files are so much and the process will take much long, I decided to implement multithreading. Here is the code:

import os, threading

def loop():
    for x in range(800):
        inizialize().start()

class inizialize(threading.Thread):

    def run(self):
        self.program()

    def program(self):
        n = list(range(778))
        n.pop(0)
        for z in n:
            Path = ("/home/user/Desktop/files/recup_dir.%s/" % (z))
            filelist = os.listdir(Path)
            print (Path)
            for x in filelist:
                try:
                    with open(Path + x, "r", encoding="ascii") as y:
                        for line in y:
                            if "firefox" in line:
                                print ("Found in %s !" % (x))
                                exit (1)
                except:
                    pass

loop()

The problem is that threads (that are 800) always do the same operations and they never pass to the next folder. They always stay on the first one (recup_dir.1).

How can I do to let threads continue searching in the other folders?

Allexj
  • 1,375
  • 6
  • 14
  • 29

1 Answers1

1

You don't distribute the work among your threads. Try passing each thread which task it should do

Here is your example - changed:

import os, threading

def loop():
    for x in range(800):
        inizialize(x).start()

class inizialize(threading.Thread):

    def __init__(self, x):
        threading.Thread.__init__(self)
        self.x = x

    def run(self):
        self.program()

    def program(self):

        z = self.x #each thread starts with a different number
        path = ("cheese.%s/" % (z))
        if not os.path.isdir(path):
            return 
        filelist = os.listdir(path)
        print (path)
        for x in filelist:
            try:
                with open(path + x, "r", encoding="ascii") as y:
                    for line in y:
                        if "firefox" in line:
                            print ("Found in %s !" % (x))
                            exit (1)
            except:
                pass

loop()
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36
  • it works! but please, since I am a newbie, could you please add more comments so I can better understand the code you posted? i'd really appreciate it.. for example I didn't get what's the usage of the first funcion (__init__) – Allexj Aug 18 '16 at 14:25
  • no, it doens't work... the system says "Found in blabla" but if I check in that file, there's not the keyword. – Allexj Aug 18 '16 at 14:51
  • try printing the line and line number to debug your problem – Yoav Glazner Aug 21 '16 at 08:56