-1

I am trying to make a script in python to search for certain type of files (eg: .txt, .jpg, etc.). I started searching around for quite a while (including posts here in SO) and I found the following snippet of code:

for root, dirs, files in os.walk(directory):
    for file in files:
        if file.endswith('.txt'):
            print file

However, I can't understand why root, dirs, files is used. For example, if I just use for file in os.walk(directory) it throws the error:

"AttributeError: 'tuple' object has no attribute 'endswith'".

What am I missing here?

Thanks in advance!

geo1230
  • 242
  • 4
  • 15

2 Answers2

2

The reason why you use root, dirs, files with os.walk is described in the docs:

For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

so, using root, dirs, files is a Pythonic way of handling this 3-tuple yield. Otherwise, you'd have to do something like:

data = os.walk('/')
for _ in data:
    root = _[0]
    dirs = _[1]
    files = _[2]

Tuples don't have an endswith attribute. Strings, which may or may not be contained in the tuple, do.

n1c9
  • 2,662
  • 3
  • 32
  • 52
  • 1
    thanks! i read the docs but i didn't quite understand that. thanks for the clarification. so, we just have root, dirs, files to keep track of our tuple and we search for the files we want in the files tuple right? – geo1230 Oct 01 '16 at 01:19
  • yep, you got it. `root` is the path name of the directory the tree is searching. `dirs` are the various dirs that `os.walk` is "walking" through, and `files` are the files contained within the dirs. :-) – n1c9 Oct 01 '16 at 01:30
0

os.walk() returns a list of results, each of which is itself a tuple.

If you assign a single name to each result, then that name will be a tuple.

Tuples do not have a .endswith() method.

John Gordon
  • 29,573
  • 7
  • 33
  • 58