的Python,os.listdir()错误
我在写这个程序的Python,os.listdir()错误
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
,并提出了AA型如下错误:
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
那是错误的含义,我该怎么办修理它?感谢提前:)
好像你通过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')
BTW,不要使用dir
作为变量名。它会影响上面的dir
功能。
你真的不需要超过一分钟! –
实际上,我将传递值的形式目录的名称更改为路径名,但它是相同的错误! – moostahfah
@ user3305277,你是否将它作为字符串对象传递? (不是'dirname',而是''dirname''或''dirname“') – falsetru
可能是因为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)
我想那是你传递的'dir'到函数不是字符串。实际上有一个名为“dir”的内置函数。 – 9000
检查是否dir是一个字符串也检查转换目录到字符串 – Sar009