0

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.

1
  • 3
    there's a better way to do this btw. os.walk Commented Aug 16, 2016 at 6:11

2 Answers 2

4

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?
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. detailed explanation and nice example,helps a lot!
0

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))

Comments

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.