How to do a recursive sub-folder search and return files in a list?

I am working on a script to recursively go through subfolders in a mainfolder and build a list off a certain file type. I am having an issue with the script. It’s currently set as follows: for root, subFolder, files in os.walk(PATH): for item in files: if item.endswith(“.txt”) : fileNamePath = str(os.path.join(root,subFolder,item)) the problem is … Read more

Using os.walk() to recursively traverse directories in Python

I want to navigate from the root directory to all other directories within and print the same. Here’s my code: #!/usr/bin/python import os import fnmatch for root, dir, files in os.walk(“.”): print root print “” for items in fnmatch.filter(files, “*”): print “…” + items print “” And here’s my O/P: . …Python_Notes …pypy.py …pypy.py.save …classdemo.py … Read more