user3281086
user3281086

Reputation: 15

Use glob.glob to search recursively for *.txt files, from current directory down

I am trying to modify below piece of code to search recursively for *.txt files from current directory (directory in which I run my script) down.

for file in glob.glob('./*.txt'):

Any advice will be much appreciated.

Upvotes: 1

Views: 615

Answers (1)

Kevin
Kevin

Reputation: 2182

You will be better off using os.walk rather than glob. It is designed for walking through directories like this. Try this code to get started:

import os
myfiles =[]
for root, dirnames, filenames in os.walk("./"):
    for filename in filenames:
        if(filename.endswith(".txt")):
            myfiles.append("%s/%s" % (root, filename))
print myfiles

Upvotes: 1

Related Questions