Reputation: 22460
I am trying to list all the TIFF files in a folder using python. I found an answer to this SO question, and adapted its code as follows:
import os
import glob
from itertools import chain
def list_tifs_rec(path):
return (chain.from_iterable(glob(os.path.join(x[0], '*.tif')) for x in os.walk(path)))
def concatStr(xs):
return ','.join(str(x) for x in xs)
But I got a runtime error about 'module' object is not callable
, when I tried to execute it as follows:
>>> l = list_tifs_rec("d:/temp/")
>>> concatStr(l)
Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 9, in concatStr
File "<string>", line 9, in <genexpr>
File "<string>", line 6, in <genexpr>
TypeError: 'module' object is not callable
I come from a C++ background and don't know Python generators very well. I googled around and didn't find close examples of this error, probably because of its generality.
Can anyone please explain the error and how to fix it?
Thanks.
Upvotes: 0
Views: 769
Reputation: 23753
An alternative would be to write a generator function that yields the file paths you need. Similar to your solution but a bit more readable.
def foo(root, file_ext):
for dirpath, dirnames, filenames in os.walk(root):
for f_name in filenames:
if f_name.endswith(file_ext):
yield os.path.join(dirpath, f_name)
usage
for name in foo(r'folder', 'tif'):
print name
files = ','.join(foo('c:\pyProjects', 'tiff'))
Upvotes: 1
Reputation: 3099
You need to call glob.iglob
(the method), not just glob
(the module), like so:
glob.iglob(os.path.join(x[0], '*.tif'))
Upvotes: 4