0

I'm writing this procedure

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

and it raises a a type error as below:

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

What's the meaning of that error and how do I fix it? Thanks in advance :)

4

2 回答 2

4

好像您将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-13T09:21:22.233 回答
0

可能是因为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)
于 2014-02-13T09:42:41.770 回答