我在写这个程序
def get_special_paths(dir):
detected_paths = []
paths = os.listdir(dir)
for path in paths:
if path == r'__\w+__':
detected_paths.append(path)
for element in detected_paths:
index = detected_path.index(element)
detected_paths[index] = os.path.abspath(element)
return detected_paths它引发一个类型错误,如下所示:
Traceback (most recent call last):
File"copyspecial.py", line 65, in <module>
get_special_paths(dir)
File"copysepcial.py", line 23, in get_special_paths
paths = os.listdir(pathname)
TypeError: coercing to Unicode: need string or buffer, builtin_function_or_method found这个错误的含义是什么,我如何纠正它?(预先谢谢:)
发布于 2014-02-13 09:21:22
似乎您将dir内置函数传递给了get_special_paths
>>> dir
<built-in function dir>
>>> os.listdir(dir)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, builtin_function_or_method found以字符串的形式传递路径。
get_special_paths('/path/to/dir')顺便说一句,不要使用dir作为变量名。它将遮挡上述dir函数。
发布于 2014-02-13 09:42:41
可能是因为这里没有定义global_path:
for element in detected_paths:
index = detected_path.index(element) # Here detected_path is undefined让它成为global_paths,并尝试:
for element in detected_paths:
index = detected_paths.index(element)https://stackoverflow.com/questions/21749800
复制相似问题