追加列表错误:类型错误,序列项o:期待字符串,发现列表
这是我的代码。基本上,我认为它应该这样工作:self.list 用来创建一个有序列表,self.contents 把一个列表变成字符串,这样我就可以在一个可滚动的窗口中显示 self.list,使用 self.plbuffer.set_text(self.contents)。然后,os.walk 会遍历在 top 中定义的目录,findall 会查找我在 self.search 中输入的内容,找到文件名中的模式,然后应该把它添加到 self.list 中。
class mplay:
def search_entry(self, widget):
self.list = []
self.contents = "/n".join(self.list)
self.plbuffer.set_text(self.contents)
search = self.search.get_text()
top = '/home/bludiescript/tv-shows'
for dirpath, dirnames, filenames in os.walk(top):
for filename in filenames:
if re.findall(filename, search):
self.list.append(os.path.join([dirpath, filename]))
这个错误是什么意思?我不能使用 os.path.join 来添加内容到 self.list 吗?
error = file "./mplay1.py" , line 77 in search_entry
self.contents = "/n".join(self.list) line
typeerror sequence item o: expecting string, list found
1 个回答
1
这个列表必须是一个字符串的列表,这样它才能正常工作:
"/n".join(["123","123","234"]) # works
"/n".join([123, 123, 234]) #error, this is int
如果你的列表是一个列表的列表,那就会出错,这可能就是你的情况:
"/n".join([[123, 123, 234],[123, 123, 234]]) # error
可以加一句 print self.list 来看看这个列表长什么样。
你说在其他地方运行得很好,可能是因为列表里的内容不一样。
另外,注意如果你把一个空列表 [] 连接起来,会得到一个空字符串,所以那行代码实际上是没什么用的。