Skip to content Skip to sidebar Skip to footer

Error In Using Os.path.walk() Correctly

So I created this Folder C:\TempFiles to test run the following code snippet Inside this folder i had two files -> nd1.txt, nd2.txt and a folder C:\TempFiles\Temp2, inside which

Solution 1:

Your issue isn't specific to Python 3, it's how os.walk() works - iterating already does the recursion to subfolders, so you can take out your recursive call:

def parseDirectory(dirname):
    global fileIndex
    for root,dirs,files in os.walk(dirname):
        for filename in files:
            nf = open(os.path.join(root,filename),'r')
            parseFile(nf,fileIndex)
            print(" --> "+ nf.name)
            fileIndex+=1
            nf.close()

By calling parseDirectory() for the dirs, you were starting another, independant walk of your only subfolder.


Post a Comment for "Error In Using Os.path.walk() Correctly"