Master Lee
Master Lee

Reputation: 43

Recursion seems not working in Python

I am writing a piece of code to recursively processing *.py files. The code block is as the following:

class FileProcessor(object):

    def convert(self,file_path):
        if os.path.isdir(file_path):
            """ If the path is a directory,then process it recursively 
                untill a file is met"""
            dir_list=os.listdir(file_path)
            print("Now Processing Directory:",file_path)

            i=1

            for temp_dir in dir_list:
                print(i,":",temp_dir)
                i=i+1
                self.convert(temp_dir)
        else:
            """ if the path is not a directory"""
            """ TODO something meaningful """

if __name__ == '__main__':
    tempObj=FileProcessor()
    tempObj.convert(sys.argv[1])

When I run the script with a directory path as argument, it only runs the first layer of the directory, the line:

self.convert(temp_dir)

seems never get called. I'm using Python 3.5.

Upvotes: 0

Views: 100

Answers (2)

acw1668
acw1668

Reputation: 47173

As temp_dir has the filename only without parent path, you should change

self.convert(temp_dir)

to

self.convert(os.path.join(file_path, temp_dir))

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54243

The recursion is happening fine, but temp_dir is not a directory so it passes control to your stub else block. You can see this if you put print(file_path) outside your if block.

temp_dir is the name of the next directory, not its absolute path. "C:/users/adsmith/tmp/folder" becomes just "folder". Use os.path.abspath to get that

self.convert(os.path.abspath(temp_dir))

Although the canonical way to do this (as mentioned in my comment on the question) is to use os.walk.

class FileProcessor(object):
    def convert(self, file_path):
        for root, dirs, files in os.walk(file_path):
            # if file_path is C:/users/adsmith, then:
            #   root == C:/users/adsmith
            #   dirs is an iterator of each directory in C:/users/adsmith
            #   files is an iterator of each file in C:/users/adsmith
            # this walks on its own, so your next iteration will be
            # the next deeper directory in `dirs`

            for i, d in enumerate(dirs):
                # this is also preferred to setting a counter var and incrementing
                print(i, ":", d)
                # no need to recurse here since os.walk does that itself
            for fname in files:
                # do something with the files? I guess?

Upvotes: 4

Related Questions