Is anyone know how to delete all files in a Folder kodi python?
#16
This code works like a dream! I'm very new to python and am stumbling through to understand and learn as I go. How can i modify that code so i can input a list of folders to remove instead of copying the code each time? Also, is there a way i can remove the contents of a folder while leaving select files and folders alone?

Would it be something like this?:

TARGETFOLDER = xbmc.translatePath(
'special://home/userdata/addon_data/folder1,
special://home/userdata/addon_data/folder2
special://home/userdata/addon_data/folder3'
)
path = TARGETFOLDER
folder = TARGETFOLDER
if os.path.exists(TARGETFOLDER):
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
donevalue = '1'
except Exception, e:
print e



Thank you very much in advance!
Reply
#17
Im trying this and it works well but it will not delete all the folders in the addon.

For example im trying to delete plugin.program.abc which has a folder inside it call resources.

It deletes the files in plugin.program.abc but nothing in plugin.program.abc/resources

Regards
Reply
#18
Code:
def remove_dir (path):
       dirList, flsList = xbmcvfs.listdir(path)
       for fl in flsList:
            xbmcvfs.delete(os.path.join(path, fl))
       for dr in dirList:
            remove_dir(os.path.join(path, dr))
       xbmcvfs.rmdir(path)
# ....
remove_dir('C:\\Users\\Thomas\\AppData\\Roaming\\Kodi\\userdata\\my_folder\\')
# OR
remove_dir(xbmc.translatePath('special://home/userdata/my_folder'))

or

Code:
def remove_dir (path):
       dflist = os.listdir(path)
       for itm in dflist:
            _path = os.path.join(path, itm)
            if os.path.isfile(_path):
                    os.remove(_path)
            else:
                    remove_dir(_path)
       os.rmdir(path)

remove_dir(TARGETFOLDER)
Reply
#19
@Taifxx Ive tried the code in your post however I'm a bit uncertain as what to do with it.
Please could you explain further.

Do I add it to the code I already have?

Regards
Reply
#20
Quite honestly, I really can't see a need for a plugin and/or script to be deleting other plugin/script directories.

If you want to remove an add-on, what is wrong with using Kodi's built in add-on installer Huh Or, not installing it in the first place Huh

I know what has gone on in the past with other third party add-on authors removing competitor add-ons and I have no wish to encourage this, or see it happening again.
Learning Linux the hard way !!
Reply
#21
I can see a need for this code for my own use. I am therefore simply asking how I might do it. Thank you.
Reply
#22
(2016-12-16, 21:37)ed_davidson Wrote: I can see a need for this code for my own use. I am therefore simply asking how I might do it. Thank you.

I'm not denying that you may indeed have a valid use case (although I still think not installing said add-on(s) is a better solution). I'm merely pointing out that this is just one step away from trashing an entire HDD. Kodi python has complete access to an entire file system and as such, perhaps some caution should be exercised (or a use case revealed) when asking such questions.
Learning Linux the hard way !!
Reply
#23
Yes, you need add this function (def remove_dir ...) to your code and call it remove_dir(...) when you need.
This function delete all files and all sub directories in target directory.

xbmc.translatePath used for changing special path on normal local path.

os.remove/xbmcvfs.delete used for remove file
os.rmdir/xbmcvfs.rmdir used for removing empty folder
You can change it to os.copy if you want copy files.
You can add file checking before os.removing if you want leave some files.
For example:
Code:
...
for fl in flsList:
    if fl in leave_list : continue
    if fl.endswith('.tmp') : continue
    os.remove(os.path.join(path, fl))
....
Reply

Logout Mark Read Team Forum Stats Members Help
Is anyone know how to delete all files in a Folder kodi python?0