문제

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 :)

도움이 되었습니까?

해결책

It seems like you passed the dir builtin function to the 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

Pass the path as string.

get_special_paths('/path/to/dir')

BTW, don't use dir as a variable name. It will shadow the above dir function.

다른 팁

May be because global_path is not defined here:

for element in detected_paths:
    index = detected_path.index(element) # Here detected_path is undefined

make it global_paths and try:

for element in detected_paths:
    index = detected_paths.index(element)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top