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!