Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top