附加列表错误typeerror序列项o:期待字符串,找到列表

问题描述:

这是我的代码。基本上我认为它应该像这样工作self.list使有序列表self.contents将列表变成一个字符串,所以我可以在self.plbuffer.set_text(self.contents)的可滚动窗口中显示self.list。那么os.walk遍历顶部定义的目录,而不是find我用self.search找到的文件中找到的模式,然后它应该被追加到self.list。附加列表错误typeerror序列项o:期待字符串,找到列表

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

error = file "./mplay1.py" , line 77 in search_entry 
      self.contents = "/n".join(self.list) line 
      typeerror sequence item o: expecting string, list found 
+0

此代码不会运行。要么给我们上课,要么清理它,以便它运行......错误是什么? – Benjamin

+0

确定类和完整的错误文本被添加,这只是整个程序的一部分。该程序本身loades当我使用search_entry函数,我得到错误 – user961559

+0

你错过了:在你的第二个最后一行... – Benjamin

列表必须是一个字符串列表,为它工作到self.list:

"/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来查看它的外观。

当你说它在别处运行良好时,可能是因为列表的内容不同。

另外,请注意,加入一个空列表[]将返回一个空字符串,以便该行无效。

+0

但它在我的程序的另一部分该特定行 – user961559

+0

可能因为在另一部分它实际上是在一个字符串上操作。你想用这条线做什么?用新的行字符分开列表中的项目并将它们连接成一个新的字符串? – Benjamin

+0

所以你说它应该self.contents = append(“\ n”,self.list) – user961559