waudy
waudy

Reputation: 11

Faster For Loops Python

so I have been working on a simple python program to iterate through each file name on the computer to check for the file type of an image or well any file type.

for i in drives:
    for root, dirs, files in os.walk(str(i.replace("\\","\\\\"))):
        #print(root,dirs)
        for file in files:
            if file.endswith(".jpeg"):
                pictures.append(file)

You see that I used a simple for loop group(i think they call this nesting I don't know) and I was wondering if there is someway to GPU accelerate this operation or can you point me in a direction so that I can improve the efficiency another way. I do note that there was a question on stack about searching filepaths for keyword but I am asking for a way to improve efficiency

Upvotes: 0

Views: 117

Answers (2)

giullianomorroni
giullianomorroni

Reputation: 29

you can use threads and disparate one thread by driver at you first line...then you will get there faster

q = Queue()
for i in drives:
    t = Thread(target=your_method, args=(q,pictures))
    t.daemon = True
    t.start()
q.join()

hope could helped

Upvotes: 0

Brian
Brian

Reputation: 1667

There really isn't any significant improvement you can make due to the limitations of disk I/O (as the comments state). However, instead of using os.walk(...), you can use os.scandir(...) to get a little more speed out of it. See this link: https://www.python.org/dev/peps/pep-0471/

Upvotes: 1

Related Questions