2

I have been coding in Python since the last 2 weeks and pretty new to it.

I have written a code to kind of emulate the way "find" command works in *NIX systems. My code works okay-ish for not so deep directories but if I start searching from the "root" directory, it takes too much time and processor heats up :D which on the other hand takes about 8 seconds using "find" cmd.

Hey I know I am kinda noob in Python now but any hint at trying to improve the search efficiency will be greatly appreciated.

Here's what I have written:

 #!/usr/bin/python3

import os

class srchx:
    file_names = []
    is_prohibit = False

    def show_result(self):
        if(self.is_prohibit):
            print("some directories were denied read-access")
        print("\nsearch returned {0} result(s)".format(len(self.file_names)))
        for _file in self.file_names:
            print(_file)

    def read_dir(self, cur_dir, srch_name, level):
        try:
            listing = os.listdir(cur_dir)
        except:
            self.is_prohibit = True
            return
        dir_list = []
        #print("-"*level+cur_dir)
        for entry in listing:
            if(os.path.isdir(cur_dir+"/"+entry)):
                dir_list.append(entry)
            else:
                if(srch_name == entry):
                    self.file_names.append(cur_dir+"/"+entry)
        for _dir in dir_list:
            new_dir = cur_dir + "/" + _dir
            self.read_dir(new_dir, srch_name, level+1)
        if(level == 0):
            self.show_result()

    def __init__(self, dir_name=os.getcwd()):
        srch_name = ""
        while(len(srch_name) == 0):
            srch_name = input("search for: ")
        self.read_dir(dir_name, srch_name, 0)




def main():
    srch = srchx()

if (__name__ == "__main__"):
    main()

Take a look at and please help me to solve this issue.

2 Answers 2

2

There is a built-in Directory-Browsing Framework called os.walk() but even os.walk() is slow, if you want to browse faster, you need access to the operating systems file-browser.

https://pypi.python.org/pypi/scandir

scandir is a solution.

Sign up to request clarification or add additional context in comments.

2 Comments

I expect scandir to be in 3.5. os.walk may be modified to use it and run faster. If you download scandir, you could probably do the substitution yourself.
@user1767754 scandir does work much better. It took a while for me to understand the code though :) thank u
0

What user1767754 said. You can't really improve the speed much using the methods you're calling. os.walk() is a bit more efficient, though. I've never used scandir (or pypi) so I can't comment.

BTW, that's rather good looking code for a noob, Marty! But there are a couple of issues with it.

  1. It's not a good idea to initialise file_names and is_prohibit like that because it makes them class variables; initialise them in __init__.

  2. You should read srch_name outside the class and pass it your class constructor. You do that by making it an arg of __init__, as described in the link above.

It's generally good policy to handle user input in the outermost parts of your code (when practical) rather than doing it in the inner parts of your code. I like to think of my user input routines as border guards that only let good input into the inner sanctum of my code. Users are unpredictable critters and there's no telling what mischief they'll get up to. :)

2 Comments

There has been a proposal to python for using os.scandir() in the future implementations, as it is really fast.
@PM 2Ring: your advices were quite valuable. I'll certainly keep them in mind next time around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.