I am trying to make a script in python to search for certain type of files (eg:
for root, dirs, files in os.walk(directory):
for file in files:
root, dirs, files
for file in os.walk(directory)
"AttributeError: 'tuple' object has no attribute 'endswith'".
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).
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 = _ dirs = _ files = _
Tuples don't have an
endswith attribute. Strings, which may or may not be contained in the tuple, do.