【发布时间】:2015-06-09 14:13:56
【问题描述】:
我正在尝试使用 python 列出文件夹中的所有 TIFF 文件。我找到了this SO question的答案,并将其代码修改如下:
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)
但是当我尝试按如下方式执行它时,我收到了关于 'module' object is not callable 的运行时错误:
>>> 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
我来自 C++ 背景,不太了解 Python 生成器。我用谷歌搜索并没有找到这个错误的密切例子,可能是因为它的普遍性。
谁能解释一下这个错误以及如何解决它?
谢谢。
【问题讨论】:
-
glob是一个模块。glob.glob()返回一个列表,glob.iglob()返回一个迭代器。见docs.python.org/2/library/glob.html。 -
谢谢大家。这很快。我会在 9 分钟内接受。