Kodi Community Forum

Full Version: TypeError: coercing to Unicode: need string or buffer, bool found
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good evening,

I'm writing a program for anyone to use. Anyone interested please feel free to use the code. Replace * with your addon. All you have to do is zip your settings(userdata/addon_data/plugin,*.*) and add them to Maintenance/* folder in Super Favourites. All works great except for the last bit where I want to delete the zip file. This is important as it checks if a zip file is present in specified folder.
I get an error
Code:
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.TypeError'>
                                            Error Contents: coercing to Unicode: need string or buffer, bool found
                                            Traceback (most recent call last):
                                              File "/home/usuario/.kodi/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/*update.py", line 71, in <module>
                                                checkifthereisupdatezip() , removezip()
                                              File "/home/usuario/.kodi/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/*update.py", line 20, in removezip
                                                shutil.rmtree(yesremove,)
                                              File "/usr/lib/python2.7/shutil.py", line 228, in rmtree
                                                if os.path.islink(path):
                                              File "/usr/lib/python2.7/posixpath.py", line 142, in islink
                                                st = os.lstat(path)
                                            TypeError: coercing to Unicode: need string or buffer, bool found
                                            -->End of Python script error report<--

here is the code
Code:
import os
import xbmc
import xbmcgui
import zipfile
import shutil
import time
import datetime
import smtplib
#IMPORT MODULES


updateziplocation = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/plugin.video.*.zip')
*userdata = xbmc.translatePath('special://home/userdata/addon_data/plugin.video.*')
now = datetime.datetime.now()
runtime = now.strftime("%d-%m-%Y %H:%M")

#functions
def removezip():
    updateziplocation = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/plugin.video.*.zip')
    yesremove = os.path.exists(updateziplocation)
    shutil.rmtree(yesremove, ignore_errors=False)

def checkifthereisupdatezip():
    now = datetime.datetime.now()
    runtime = now.strftime("%d-%m-%Y %H:%M")
    yeszip = os.path.exists(updateziplocation)


    if yeszip:
        xbmc.executebuiltin("Notification(Yes There is an Update, Over-writing previos Settings,()")
        old*settings = xbmc.translatePath('special://home/userdata/addon_data/plugin.video.*')
        shutil.rmtree(old*settings, ignore_errors=False)
        xbmc.executebuiltin("Notification(Old * Settings Removed, Installing new Settings,()")
        fh = open(updateziplocation, 'rb')
        z = zipfile.ZipFile(fh)
        for name in z.namelist():
            z.extract(name, old*settings)
        fh.close()
        xbmcgui.Dialog().ok(
        "Thankyou for using * Update",
        "Your * settings have been updated",
        "Read the news feed for information about",
        "when * settings are next updated")
    else:
        YESNOWINDOW = xbmcgui.Dialog().yesno(
        "There are no new settings at the moment",
        "ONLY IF...* has stopped working",
        "Would you like to send a message to Admin",
        "Asking for an update?",
        "No Thanks",
        "Yes Please"
        )
        if YESNOWINDOW:
            mailserver = smtplib.SMTP("smtp.mail.com",587)
            mailserver.ehlo()
            mailserver.starttls()
            mailserver.login('somemail','somepwd')
            mailserver.sendmail('somemail','somemail', ' * Update Request ' + runtime )
            mailserver.close()
            xbmcgui.Dialog().ok(
            "Thankyou for using * Update",
            "An Email has been sent to Admin",
            "Requesting * settings update",
            "")
        else:
            xbmcgui.Dialog().ok(
            "Thankyou for using * Update",
            "Nothing on your system has changed",
            "Read the news feed for information about",
            "when * settings are updated next")

checkifthereisupdatezip() , removezip()

Can anyone please shed light on the error. Thanks a million.
yesremove is a boolean (True / False), it's not a path.

most likely you want to use this:
Code:
if yesremove:
    shutil.rmtree(updateziplocation, ignore_errors=False)
(2015-11-24, 00:06)ronie Wrote: [ -> ]yesremove is a boolean (True / False), it's not a path.

most likely you want to use this:
Code:
if yesremove:
    shutil.rmtree(updateziplocation, ignore_errors=False)

Thank you ronie as always great help. should i put
Code:
else:
    some code here to complete the operation
(2015-11-24, 00:06)ronie Wrote: [ -> ]yesremove is a boolean (True / False), it's not a path.

most likely you want to use this:
Code:
if yesremove:
    shutil.rmtree(updateziplocation, ignore_errors=False)

Hi Ronie,

thanks for your help, almost there now. Ill post full code once its working 100% for anyone to use.
I'm now getting this in the log.
Code:
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.OSError'>
                                            Error Contents: [Errno 20] Not a directory: '/home/usuario/.kodi/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/plugin.video.*.zip'
                                            Traceback (most recent call last):
                                              File "/home/usuario/.kodi/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/*update.py", line 78, in <module>
                                                removezip()
                                              File "/home/usuario/.kodi/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/*update.py", line 22, in removezip
                                                shutil.rmtree(updateziplocation, ignore_errors=False)
                                              File "/usr/lib/python2.7/shutil.py", line 239, in rmtree
                                                onerror(os.listdir, path, sys.exc_info())
                                              File "/usr/lib/python2.7/shutil.py", line 237, in rmtree
                                                names = os.listdir(path)
                                            OSError: [Errno 20] Not a directory: '/home/usuario/.kodi/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/plugin.video.*.zip'
                                            -->End of Python script error report<--

The snippet of code it belongs to is this
Code:
#functions
def removezip():
    updateziplocation = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/*update/plugin.video.*.zip')
    yesremove = os.path.exists(updateziplocation)
    if yesremove:
        shutil.rmtree(updateziplocation, ignore_errors=False)
    else:
        xbmc.executebuiltin("ActivateWindow(10000,return)")

and here

Code:
#execute
checkifthereisupdatezip()
time.sleep(3)
removezip()

The folder /Maintenance/*update
and the file /Maintenance/*update/plugin.video.*.zip

are both 100% there.

Thanks a million
with shutil.rmtree() you can only delete directories.
if you want to delete a file, use os.remove() instead.
SOLVED

Code:
def removezip():
    updateziplocation = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites/Maintenance/itvupdate')
    os.chdir(updateziplocation)
    files=glob.glob('*.zip')
    for filename in files:
        os.unlink(filename)

Thank you ronie for your help