0

I am modifying a wizard in Kodi and I would like the wizard to delete all folders contained within the "addons" directory without deleting my wizard.

The directory of the folder will be using the "special://" function built into Kodi. I would like to delete everything inside of "special://home/addons" except for the folder named "plugin.video.spartan0.12.0"

I know that python needs to use "xbmc.translatePath" function to recognize the folder path, but I don't know how to delete everything in the folder without deleting "plugin.video.spartan0.12.0"

Any help would be appreciated.


Here is what I currently have

import os
dirPath = "C:\Users\Authorized User\AppData\Roaming\Kodi\addons"
fileList = os.listdir(dirPath)
for fileName in fileList:
    os.remove(dirPath+"/"+fileName) 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tye Beach
  • 1
  • 1
  • 1
  • 2
    Something as simple as `if folder_name != "plugin.video.spartan0.12.0": # delete the rest` should work – OneCricketeer Jan 26 '16 at 20:00
  • I was using shutil.rmtree before and I see know that it deletes the whole folder. I want to delete everything inside of the addons folder, but not delete the addons folder itself. How would I do that? – Tye Beach Jan 26 '16 at 21:26
  • `os.listdir(addon_path)` will return a list of file & directory names for the system path given by the variable `addon_path`. You are free to delete to your heart's content within a for-loop over that data – OneCricketeer Jan 26 '16 at 21:36
  • What would you change/add in this example to delete all of the contents inside of the addons folder (except for plugin.video.spartan0.12.0) I can't seem to figure out how to even delete ALL the contents inside of a folder. I can delete the whole folder, but for some reason I can't figure out how to delete everything else inside the folder. I know this code doesn't work, but what do I need to do? import os dirPath = "C:\Users\Authorized User\AppData\Roaming\Kodi\addons" fileList = os.listdir(dirPath) for fileName in fileList: os.remove(dirPath+"/"+fileName) – Tye Beach Jan 26 '16 at 21:56
  • Well, you should be using backslashes ( \ ) for windows paths, or even better, use `os.path.join(dirPath, fileName)`. Other than that, just do an if-statement on `fileName` for the plugin name and I think you're good. – OneCricketeer Jan 26 '16 at 22:12

1 Answers1

1

This is probably overkill (and a little sloppy), but it worked for me:

import os
import shutil

#----------------------------------------------------------------------
def remove(path):
    """
    Remove the file or directory
    """
    if os.path.isdir(path):
        try:
            shutil.rmtree(path)
        except OSError:
            print "Unable to remove folder: %s" % path
    else:
        try:
            if os.path.exists(path):
                os.remove(path)
        except OSError:
            print "Unable to remove file: %s" % path


#----------------------------------------------------------------------
def cleanup(dirpath, folder_to_exclude):
    for root, dirs, files in os.walk(dirpath, topdown=True):
        for file_ in files:
            full_path = os.path.join(root, file_)
            if folder_to_exclude not in full_path:
                print 'removing -> ' + full_path
                remove(full_path)
        for folder in dirs:
            full_path = os.path.join(root, folder)
            if folder_to_exclude not in full_path:
                remove(full_path)

if __name__ == '__main__':
    cleanup(r'c\path\to\addons', 'plugin.video.spartan0.12.0')
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88