Python error TypeError: must be string or buffer, not instance [closed]

只愿长相守 提交于 2019-12-24 09:07:10

问题


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

回答1:


It's hard to say for sure without seeing the stack trace, but I suspect this line:

imageFile.write(urllib.urlopen(filename)).read()

Should instead be:

imageFile.write(urllib.urlopen(filename).read())

Incidentally, you don't need the imageFile.close() line, because the with statement closes the file for you automatically.



来源:https://stackoverflow.com/questions/40894896/python-error-typeerror-must-be-string-or-buffer-not-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!