Given the following piece of python code:
for root, dirs, files in os.walk(directory):
for filename in fnmatch.filter(files, \'*.png\'):
pass
This would be a better way, perhaps because you are not calling +
repeatedly and using a tuple
instead of list
.
for root, dirs, files in os.walk(directory):
for extension in ('*.jpg', '*.jpeg', '*.gif', '*.png'):
for filename in fnmatch.filter(files, extension):
pass
A tuple
is better because you are not going to modify the extension once you have created them. You are just using to iterate over them.