Search code examples
pythonxbmcshutilos.pathkodi

Folder Missing But No confirmation


Ok, Hi everyone, this is my code to delete a specified folder, It is cross platform compatible and designed for Kodi. I've had help from the devs there but there is a bit of code missing, more information at the bottom of the code.

    import xbmcgui
    import xbmc
    import os
    import shutil

    TARGETFOLDER = xbmc.translatePath('special://home/userdata/addon_data/01')
    yesnowindow = xbmcgui.Dialog().yesno("This Will Delete a folder","Click yes to delete","Click No to exit")
    NOOPTION = xbmc.executebuiltin("ActivateWindow(10000,return)")


    if yesnowindow:
        os.path.exists(TARGETFOLDER)
        if os.path.exists(TARGETFOLDER):
            shutil.rmtree(TARGETFOLDER),  xbmc.executebuiltin("Notification(Folder has been deleted, All done,()"), xbmc.executebuiltin("ActivateWindow(10000,return)")
        else: 
            NOOPTION

If Yes button is pressed and TARGETFOLDER does not exist, I want it to do this code, I know it must have to do with os.path.exists

and in Lamens terms

if os.path.exists(TARGETFOLDER): shutil.rmtree(TARGETFOLDER), and if os.path.exists(TARGETFOLDER) = false then

    xbmc.executebuiltin("Notification(Ok, All done,()")

Thanks for any help you can give me.


Solution

  • based off your example code and what I understand from the xbmcgui docs:

    import xbmcgui
    import xbmc
    import os
    import shutil
    
    TARGETFOLDER = xbmc.translatePath(
        'special://home/userdata/addon_data/01'
        )
    YESNOWINDOW = xbmcgui.Dialog().yesno(
        "This Will Delete a folder",
        "Click yes to delete",
        "Click No to exit")
    
    
    if YESNOWINDOW:
        _MSG = None
        if os.path.exists(TARGETFOLDER):
            try:
                shutil.rmtree(TARGETFOLDER, ignore_errors=False)
                xbmc.executebuiltin("Notification(Folder has been deleted, All done,()")
                xbmc.executebuiltin("ActivateWindow(10000,return)")
            except OSError as rmerr:
                _MSG = ("Error with delete dir: {}".format(rmerr))
            except Exception as err:
                _MSG = ("Error with XBMC call: {}".format(err))
        else:
            _MSG = ("Folder {} does not appear to be a directory"
                    .format(TARGETFOLDER))
        if _MSG:
            xbmc.executebuiltin("Notification({},()".format(_MSG)) # ***
            xbmc.executebuiltin("ActivateWindow(10000,return)")
    

    try this and report back what is borked. I didn't have a text box that I could get the xbmc libs on to verify this.