Python 错误类型错误:必须是字符串或缓冲区,而不是实例 [英] Python error TypeError: must be string or buffer, not instance
问题描述
我正在尝试下载 QListWidget 中列出的一些图像我正在将链接传递给 urllib 但它给了我 TypeError: must be string or buffer, not instance 这个错误.我尝试在这里查找,但找不到任何解决方案,这是我的代码.谢谢
i am trying to download some images which are listed in QListWidget i am passing the links to the urllib but its giving me TypeError: must be string or buffer, not instance this error. I tried looking up here but couldn't find any solution here is my code. Thanks
def downloadStuff(self):
files = self.listWidget.selectedItems()
for filename in files:
filename = filename.text()
filename = str(filename)
print filename
xfilename = filename.split('/')[-1]
with open('D:/'+xfilename,'wb') as imageFile:
print filename
imageFile.write(urllib.urlopen(filename)).read()
imageFile.close()
推荐答案
如果没有看到堆栈跟踪很难确定,但我怀疑这一行:
It's hard to say for sure without seeing the stack trace, but I suspect this line:
imageFile.write(urllib.urlopen(filename)).read()
应该是:
imageFile.write(urllib.urlopen(filename).read())
顺便说一句,您不需要 imageFile.close()
行,因为 with
语句会自动为您关闭文件.
Incidentally, you don't need the imageFile.close()
line, because the with
statement closes the file for you automatically.
这篇关于Python 错误类型错误:必须是字符串或缓冲区,而不是实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!