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

2
  • 1
    I suppose that what you pass as dir into the function is not a string. Actually there is a built-in function named exactly so, dir. Commented Feb 13, 2014 at 9:22
  • check if dir is a string also check converting dir to string Commented Feb 13, 2014 at 9:22

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

8 Comments

you really don't need more than a minute!
Actually, I changed the name of passed value form dir to pathname but it's the same error!!
@user3305277, Did you pass it as string object? (not dirname, but 'dirname' or "dirname")
@user3305277, Could you update your question? (adding calling part of the code)
@user3305277, Please add the calling part in the question.
|
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.