0

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.

Simon Jeal
  • 153
  • 2
  • 4
  • 14

3 Answers3

0

I don't know how crossplatform this is, but to me your question screams for try/except. Perhaps you can flesh this out to suit your needs:

import shutil
my_folder = 'foobar'
try:
    shutil.rmtree(my_folder)
    print 'folder deleted'
except OSError, e:
    the_error = str(e)
    if '[Errno 20]' in the_error:
        print my_folder, 'is not a directory!'
    elif '[Errno 2]' in the_error:
        print my_folder, 'did not exist!'
    else:
        print the_error
Abd Azrad
  • 301
  • 2
  • 10
0

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.

cgseller
  • 3,875
  • 2
  • 19
  • 21
0

THANKYOU, yep it works great one thing was a , missing in line 27 after {}

_MSG = ("Folder {} does not appear to be a directory"

to dispaly the notification properly.

I've uploaded the code to pastebin http://pastebin.com/BS3VQLbb

with comments on each line. It would be great if anyone has a chance to take a look to see if I'm understanding the code correctly.

I do have a couple of questions about the code, can I ask them here? as it seems I'm not allowed to ask for help with it. If its ok to ask please let me know. Thanks again,

Simon Jeal
  • 153
  • 2
  • 4
  • 14